我不明白破坏的工作方式......
我有一个类 SubWindow,它是 Tk.Toplevel 的子类如果这个子窗口有一个内部小部件
self.label = Tk.Label(master=self, text='这是子窗口:%s!!!!'%self)
当我销毁子窗口时,它不会死,它仍然活着(我知道这看着子窗口数组)
销毁子窗口的唯一方法是调用
删除自我标签
在销毁方法中。
import Tkinter as Tk
import weakref
root = Tk.Tk()
subwindows = []
def subwindow():
subwindows.append(weakref.ref(SubWindow(root)))
def list_subwindows():
print 'Subwindows: ',
for subwindow in subwindows:
sw = subwindow()
if sw:
print sw,
print ''
class SubWindow(Tk.Toplevel):
def __init__(self, window):
Tk.Toplevel.__init__(self, master = window)
self.label = Tk.Label(master=self, text='This is Sub Window: %s!!!!'%self)
self.label.pack()
# self.bind('<Destroy>', self._destroy)
def _destroy(self, event):
try:
del self.label
except:
pass
bStartWindow = Tk.Button(master=root, text='Start Sub Window',
command=subwindow)
bStartWindow.pack()
bListSubwindows = Tk.Button(master=root, text='List Active subwindows',
command = list_subwindows)
bListSubwindows.pack()
root.mainloop()
为什么子窗口有内部小部件时不会被销毁?