在Python中的Tkinter.Tk窗口时,可以为清除任务覆盖哪种方法? [英] Which method can I override for cleanup task when a Tkinter.Tk window in Python?

查看:252
本文介绍了在Python中的Tkinter.Tk窗口时,可以为清除任务覆盖哪种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class MainGUI(Tkinter.Tk):
    # some overrides

# MAIN 

gui = MainGUI(None)
gui.mainloop()

但是当窗口位于由用户关闭。我可以重写Tkinter.Tk中的哪个方法?

But I need to do some cleanup when the window is closed by the user. Which method in Tkinter.Tk can I override?

推荐答案

您可以设置一个绑定,该绑定在窗口销毁时会触发。绑定到< Destroy> 或为WM_DELETE_WINDOW添加协议处理程序。

You can set up a binding that gets fired when the window is destroyed. Either bind to <Destroy> or add a protocol handler for WM_DELETE_WINDOW.

例如:

def callback():
    # your cleanup code here

...
root.protocol("WM_DELETE_WINDOW", callback)

这篇关于在Python中的Tkinter.Tk窗口时,可以为清除任务覆盖哪种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆