tkinter - 无法隐藏嵌套的顶层 window
tkinter - Cant hide nested toplevel window
我在另一个 class 中创建了一个 TopLevel class 的实例,但不能 "withdraw()" child window
来自 parent.
的电话
import tkinter as tk
class Table(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.var_new_t=tk.Toplevel(self.master)
self.t2=Table_2(self.var_new_t)
#Goal is to create a child window and immediately hide it.
self.t2.hide_me()
#self.t2.withdraw() ##Tried a few differnt ways
class Table_2(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.master = master
label = tk.Label(self.master, bg='green', text='Second Table')
label.grid()
def hide_me(self):
self.master.withdraw()
root = tk.Tk()
n= Table(root)
tk.Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
我尝试了一些其他变体,但无济于事。
您的代码正在创建两个 windows。考虑这段代码:
Table
是顶层 window 因为它继承自 Toplevel
。所以,这一行创建 Table
window:
tk.Toplevel.__init__(self,master)
然后,当您这样做时,您会创建 另一个 window:
self.var_new_t=tk.Toplevel(self.master)
如果您继承自 Toplevel
,您通常不应在其中创建第二个 Toplevel
,除非您明确打算创建两个 windows。
您的代码需要如下所示:
class Table(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.t2=Table_2(self)
self.t2.hide_me()
class Table_2(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.master = master
label = tk.Label(self, bg='green', text='Second Table')
label.grid()
def hide_me(self):
self.withdraw()
我在另一个 class 中创建了一个 TopLevel class 的实例,但不能 "withdraw()" child window 来自 parent.
的电话import tkinter as tk
class Table(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.var_new_t=tk.Toplevel(self.master)
self.t2=Table_2(self.var_new_t)
#Goal is to create a child window and immediately hide it.
self.t2.hide_me()
#self.t2.withdraw() ##Tried a few differnt ways
class Table_2(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.master = master
label = tk.Label(self.master, bg='green', text='Second Table')
label.grid()
def hide_me(self):
self.master.withdraw()
root = tk.Tk()
n= Table(root)
tk.Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
我尝试了一些其他变体,但无济于事。
您的代码正在创建两个 windows。考虑这段代码:
Table
是顶层 window 因为它继承自 Toplevel
。所以,这一行创建 Table
window:
tk.Toplevel.__init__(self,master)
然后,当您这样做时,您会创建 另一个 window:
self.var_new_t=tk.Toplevel(self.master)
如果您继承自 Toplevel
,您通常不应在其中创建第二个 Toplevel
,除非您明确打算创建两个 windows。
您的代码需要如下所示:
class Table(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.t2=Table_2(self)
self.t2.hide_me()
class Table_2(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self,master)
self.master = master
label = tk.Label(self, bg='green', text='Second Table')
label.grid()
def hide_me(self):
self.withdraw()