覆盖 Tkinter “X"按钮控件(关闭窗口的按钮) [英] Overriding Tkinter "X" button control (the button that close the window)

查看:26
本文介绍了覆盖 Tkinter “X"按钮控件(关闭窗口的按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下我创建的 close Button 时,会在退出之前执行一些任务.但是,如果用户单击窗口右上角的 [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.

如果这是一个基本的保存窗口,你为什么要重新发明轮子?Tk 有一个用于此目的的 tkFileDialog.

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 “X"按钮控件(关闭窗口的按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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