2015-02-06 96 views
1

在下面的代码中,tk不是由函数launch()创建的Toplevel对象的父代。但是,当我使用tk.destroy()销毁tk时,Toplevel窗口消失。为什么Tkinter Toplevel物体被破坏?

Toplevel寡妇被摧毁了吗?如果是这样,Toplevel.destroy()如何被调用?

from tkinter import * 


def launch(): 
    Toplevel() 

tk = Tk() 

frame = Frame(tk, relief="ridge", borderwidth=2) 
frame.pack(fill="both", expand=1) 
label = Label(frame, text="Hello, World") 
label.pack(fill=X, expand=1) 

button1 = Button(frame, text="Exit", command=tk.destroy) 
button2 = Button(frame, text="Launch", command=launch) 

button1.pack(side="bottom") 
button2.pack(side="bottom") 

tk.mainloop() 

回答

5

让您的应用程序运行的是Tk实例的主循环,它是所有小部件的父项。当你销毁它时,所有的部件也被销毁。


记住,对于每个Tk的情况下,有一个相关的的Tcl解释,我会尽量给上,当你关闭一个窗口会发生什么情况的基础上,传统知识文档串的更详细的解答以及模块的相关类和方法。

Tk派生自2个类别:杂项Wm。在杂项类中,你可以找到的接口,并为文档退出方法:

def quit(self): 
    """Quit the Tcl interpreter. All widgets will be destroyed.""" 
    self.tk.quit() 

您可以在tk类的destroy方法找到如下:

def destroy(self): 
    """Destroy this and all descendants widgets. This will 
    end the application of this Tcl interpreter.""" 

破坏方法在Tk类调​​用也在某个点,破坏方法杂项 cl屁股,在那里你还可以找到另一个文档:

def destroy(self): 
    """Internal function. 
    Delete all Tcl commands created for 
    this widget in the Tcl interpreter.""" 

哪个不说,还Tcl解释停止(像退出上述方法)。

构建Tk实例时,调用名为_loadtk的方法。在这种方法中,它被设置在在关闭窗口Tk的:

self.protocol("WM_DELETE_WINDOW", self.destroy) 

,你可以看到,destroy(而不是退出)与窗口的关闭事件相关联。


这一切都意味着,当你关闭该窗口,Tk的实例及其所有子被破坏,但Tcl解释不停止。

3

Tkinter.Tk是所有的Tkinter窗口的大波帕鼻祖。它运行逻辑并与操作系统进行通信。当它走了 - 他们都走了。