重写的Tkinter英寸×"按钮控件(按钮关闭窗口) [英] Overriding Tkinter "X" button control (the button that close the window)

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

问题描述

当用户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.

如果这是一个基本保存窗口,你为什么要重新发明轮子?
塔卡 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英寸×"按钮控件(按钮关闭窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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