所以我只想让这个窗口成为一个始终位于顶部的文本小部件。我试图添加这个小部件,但它不断创建另一个窗口......
import tkinter as tk
from tkinter import ttk
class App(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.pack()
hello="hello"
self.tb = tk.Text(self)
self.tb.pack(expand=1, fill = tk.BOTH)
self.tb.insert(tk.END, hello)
topLevelWindow = tk.Toplevel(self)
# Make topLevelWindow remain on top until destroyed, or attribute changes.
topLevelWindow.attributes('-topmost', 'true')
if __name__ == "__main__":
root = tk.Tk()
main = App(root)
root.mainloop()
答案 0 :(得分:0)
如果您希望文本窗口小部件处于活动状态,则需要调用它:
self.tb.focus_set()
你正在做的是创建一个Toplevel
窗口小部件,它是一个窗口,它位于桌面的上面所有窗口的最上面,你应该删除:
topLevelWindow = tk.Toplevel(self)
# Make topLevelWindow remain on top until destroyed, or attribute changes.
topLevelWindow.attributes('-topmost', 'true')
如果您还想要整个窗口,则可以根据this answer在{main}中使用root
代替this source:
root.call('wm', 'attributes', '.', '-topmost', '1')
最后要:
import tkinter as tk
from tkinter import ttk
class App(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.pack()
hello="hello"
self.tb = tk.Text(self)
self.tb.pack(expand=1, fill = tk.BOTH)
self.tb.insert(tk.END, hello)
self.tb.focus_set()
if __name__ == "__main__":
root = tk.Tk()
root.call('wm', 'attributes', '.', '-topmost', '1')
main = App(root)
root.mainloop()
另外,如果你想让其他小部件不可聚焦:
widget.config(takefocus=False)