apa*_*apa 1 python destructor tkinter
class MainGUI(Tkinter.Tk):
# some overrides
# MAIN
gui = MainGUI(None)
gui.mainloop()
Run Code Online (Sandbox Code Playgroud)
但是当用户关闭窗口时,我需要进行一些清理。我可以覆盖Tkinter.Tk中的哪个方法?
您可以设置一个在窗口销毁时触发的绑定。绑定到<Destroy>或添加WM_DELETE_WINDOW的协议处理程序。
例如:
def callback():
# your cleanup code here
...
root.protocol("WM_DELETE_WINDOW", callback)
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您希望在销毁特定的小部件时执行某项操作,则可以考虑覆盖destroy()方法。请参见以下示例:
class MyButton(Tkinter.Button):
def destroy(self):
print "Yo!"
Tkinter.Button.destroy(self)
root = Tkinter.Tk()
f = Tkinter.Frame(root)
b1 = MyButton(f, text="Do nothing")
b1.pack()
f.pack()
b2 = Tkinter.Button(root, text="f.destroy", command=f.destroy)
b2.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
当按下按钮“ b2”时,框架“ f”被破坏,子项“ b1”和“ Yo!” 打印。
| 归档时间: |
|
| 查看次数: |
2549 次 |
| 最近记录: |