简体   繁体   English

为什么Tkinter顶级对象被破坏?

[英]Why is Tkinter Toplevel object being destroyed?

In the code below, tk is not the parent of the Toplevel object that is created by the function launch() . 在下面的代码中, tk不是函数launch()创建的Toplevel对象的父级。 However, when I destroy tk using tk.destroy() , the Toplevel window vanishes. 但是,当我使用tk.destroy()销毁tktk.destroy()窗口消失了。

Is the Toplevel widow being destroyed? 顶级寡妇被摧毁了吗? If so, how is Toplevel.destroy() being called? 如果是这样,那么如何调用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()

What keeps your application running is the mainloop of the Tk instance, which is the parent of all widgets. 使您的应用程序运行的是Tk实例的mainloop,它是所有小部件的父级。 When you destroy it, all the widgets are also destroyed. 销毁它时,所有小部件也会被销毁。


Keeping in mind that for each Tk instance, there's an associated Tcl interpreter, I will try to give a more detailed answer on what happens when you close a window, based on the docs strings of the Tk and associated classes and methods of the tkinter module. 请记住,对于每个Tk实例,都有一个关联的Tcl解释器,我将根据Tk的docs字符串以及tkinter模块的关联类和方法,尝试给出关闭窗口时会发生的更详细的答案。 。

Tk derives from 2 classes: Misc and Wm . Tk源自2类: MiscWm In the Misc class, you can find the interface and the documentation for the quit method: 在Misc类中,您可以找到quit方法的接口和文档:

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

You can find under the destroy method of the Tk class the following: 您可以在Tk类的destroy方法下找到以下内容:

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

The destroy method in the Tk class calls also, at certain point, the destroy method of the Misc class, and there you can find also another documentation: 在某些时候, Tk类中的destroy方法还调用Misc类的destroy方法,在那里您还可以找到另一个文档:

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

Which does not say that also the Tcl interpreter is stopped (like in the quit method described above). 这并不表示Tcl解释器也已停止(就像上述quit方法一样)。

When constructing a Tk instance, a method called _loadtk is called. 构造Tk实例时,将调用一个名为_loadtk的方法。 In this method, it is set the protocol when the Tk window is closed: 在此方法中,设置了关闭Tk窗口时的protocol

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

as you can see, destroy (and not quit ) is associated with the closing event of the window. 如您所见, destroy (而不是quit )与窗口的关闭事件相关。


This all means that when you close the window, the Tk instance and all its children are destroyed, but the Tcl interpreter is not stopped. 这一切都意味着,当您关闭窗口时,Tk实例及其所有子代将被销毁,但Tcl解释器不会停止。

Tkinter.Tk is the big poppa granddaddy of all tkinter windows. Tkinter.Tk是所有Tkinter.Tk窗口的大Tkinter.Tk It runs the logic and communicates with the OS. 它运行逻辑并与OS通信。 When it goes -- they all go. 一切顺利-他们全都去了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM