问题描述
当用户presses一个的关闭的按钮
我创建一些任务退出前执行。但是,如果用户点击窗口右上角的 [X]
按钮关闭该窗口,我不能执行这些任务。
When the user presses a close Button
that I created, some tasks are performed before exiting. However, if the user clicks on the [X]
button in the top-right of the window to close the window, I cannot perform these tasks.
我怎么可以重写会发生什么,当用户点击 [X]
按钮?
How can I override what happens when the user clicks [X]
button?
推荐答案
听起来好像你保存窗口应该是。
It sounds as if your save window should be modal.
如果这是一个基本保存窗口,你为什么要重新发明轮子?塔卡
有的用于这一目的。
If this is a basic save window, why are you reinventing the wheel?Tk
has a tkFileDialog
for this purpose.
如果你想要的是覆盖销毁窗口的默认行为,你可以简单地做:
If what you want is to override the default behaviour of destroying the window, you can simply do:
root.protocol('WM_DELETE_WINDOW', doSomething) # root is your root window
def doSomething():
# check if saving
# if not:
root.destroy()
此方式,可以拦截的destroy()
打电话时,有人关闭窗口(通过任何方式),做你喜欢的事情。
This way, you can intercept the destroy()
call when someone closes the window (by any means) and do what you like.
这篇关于重写的Tkinter英寸×"按钮控件(按钮关闭窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!