【发布时间】:2010-11-15 02:17:07
【问题描述】:
我正在使用 Tkinter 制作一个小型应用程序。我想在关闭窗口时调用的函数中清理一些东西。我正在尝试将我的窗口的 close 事件 与该函数绑定。不知道有没有可能,对应的顺序是什么。
Python 文档说:See the bind man page and page 201 of John Ousterhout’s book for details。
很遗憾,我手中没有这些资源。有人知道可以绑定的事件列表吗?
另一种解决方案是清理我的 Frame 类的 __del__ 中的所有内容。由于未知原因,它似乎从未被调用过。有谁知道可能是什么原因?一些循环依赖?
一旦我添加了一个控件(在下面的代码中取消注释),__del__ 就不再被调用。有解决这个问题的办法吗?
from tkinter import *
class MyDialog(Frame):
def __init__(self):
print("hello")
self.root = Tk()
self.root.title("Test")
Frame.__init__(self, self.root)
self.list = Listbox(self, selectmode=BROWSE)
self.list.pack(fill=BOTH, expand=1)
self.pack(fill=BOTH, expand=1)
def __del__(self):
print("bye-bye")
dialog = MyDialog()
dialog.root.mainloop()
【问题讨论】:
标签: python python-3.x tkinter