我无法设置顶级标题 [英] I can not set title of top level
问题描述
我想为TopLevel设置标题,但是TopLevel显示Root的标题.我认为我的下一个脚本与TkInter文档中的示例相对应,但结果却很糟糕.您能不能解释一下,为什么我在 class AppTop 中设置 master.title ='Top'不会为TopLevel设置新标题?
I want to set title for TopLevel, but TopLevel shows title of Root. I think that my next script corresponds with examples from TkInter documentation, but gives me bad result. Cann You explain me, why my setting master.title = 'Top' in class AppTop does not set new title for TopLevel?
import tkinter as tk
class AppTop(tk.Frame):
def __init__(self, master):
mon_h = 900
mon_w = 1250
master.title = 'Top'
tk.Frame.__init__(self, master)
master.minsize(height = 900, width = 600)
fr_button = tk.Frame(master)
fr_button.place(relx=0.01, rely=0.06)
butArrowPlus = tk.Button(fr_button, text=">", height = 1, width = 20, command=self.Cmd)
butArrowPlus.grid(column= 1, row= 1)
return
def Cmd(self):
return
class Application(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
frRoot = tk.Frame(master, width=700, height=400, bd=2)
frRoot.place(relx=0.1, rely=0.1, anchor="nw")
butIllumBall = tk.Button(frRoot, text= 'Light Ball', height = 1, width = 20, command=self.cmd_illuminated_ball)
butIllumBall.grid(column= 0, row= 0, pady=10)
master.minsize(height = 250, width = 300)
master.title('Root')
def cmd_illuminated_ball(self):
top = tk.Toplevel()
top.transient(self.master)
top.grab_set()
app = AppTop(master = top)
app.mainloop()
return
wndRoot = tk.Tk()
appapp = Application(master=wndRoot)
appapp.mainloop()
推荐答案
您尝试使用以下方法设置顶级标题:
You try to set Toplevel title with:
master.title = 'Top'
但正确的语法是:
master.title('Top')
还有其他几件事情:对于顶级"窗口,您不需要额外的主循环.从代码中,您似乎认为Toplevel是一个新应用程序,并使用app = AppTop(master = top)
对其进行了实例化.但这只是一个在appapp.mainloop()
下运行的新窗口.
There are a couple of additional things: You do not need an additional mainloop for the Toplevel window. From the code it looks like you think that the Toplevel is a new application, instantiating it with app = AppTop(master = top)
. But it's just a new window which runs under the appapp.mainloop()
.
AppTop()
从tk.Frame()继承,但是您从不使用它.相反,您可以将所有小部件直接放在顶层"(主)窗口中. Application()
也是如此.
AppTop()
inherits from tk.Frame() but you never use it. Instead you put all the widgets directly in the Toplevel (master) window. Same goes for Application()
as well.
这篇关于我无法设置顶级标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!