【问题标题】:I can not set title of top level我无法设置顶级标题
【发布时间】:2019-04-10 20:23:12
【问题描述】:

我想为 TopLevel 设置标题,但 TopLevel 显示 Root 的标题。我认为我的下一个脚本与 TkInter 文档中的示例相对应,但结果很糟糕。你能解释一下,为什么我在 class AppTop 中的设置 master.title = 'Top' 没有为 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()

【问题讨论】:

    标签: tkinter toplevel


    【解决方案1】:

    您尝试设置顶级标题:

    master.title = 'Top'
    

    但正确的语法是:

    master.title('Top')
    

    还有一些额外的东西:您不需要为顶层窗口添加额外的主循环。从代码看来,您认为 Toplevel 是一个新应用程序,使用app = AppTop(master = top) 对其进行实例化。但它只是一个在appapp.mainloop() 下运行的新窗口。

    AppTop() 继承自 tk.Frame() 但你从不使用它。相反,您将所有小部件直接放在顶层(主)窗口中。 Application() 也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2021-09-13
      • 1970-01-01
      相关资源
      最近更新 更多