Python 覆盖 Tkinter“X”按钮控件(关闭窗口的按钮)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3295270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overriding Tkinter "X" button control (the button that close the window)
提问by erkangur
When the user presses a closeButtonthat 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.
当用户按下我创建的关闭Button按钮时,会在退出之前执行一些任务。但是,如果用户单击[X]窗口右上角的按钮关闭窗口,我将无法执行这些任务。
How can I override what happens when the user clicks [X]button?
如何覆盖用户单击[X]按钮时发生的情况?
采纳答案by Nick Presta
It sounds as if your save window should be modal.
听起来好像您的保存窗口应该是modal。
If this is a basic save window, why are you reinventing the wheel?
Tkhas a tkFileDialogfor this purpose.
如果这是一个基本的保存窗口,你为什么要重新发明轮子?
Tk有一个tkFileDialog为此目的。
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()
This way, you can intercept the destroy()call when someone closes the window (by any means) and do what you like.
这样,您可以destroy()在有人关闭窗口时(以任何方式)拦截呼叫并做您喜欢的事情。
回答by erkangur
I found a reference on Tkinter here. It's not perfect, but covers nearly everything I ever needed. I figure section 30.3 (Event types) helps, it tells us that there's a "Destroy" event for widgets. I suppose .bind()ing your saving jobs to that event of your main window should do the trick.
我在这里找到了关于 Tkinter 的参考。它并不完美,但几乎涵盖了我所需要的一切。我认为第 30.3 节(事件类型)有帮助,它告诉我们小部件有一个“销毁”事件。我想 .bind() 将您的保存作业绑定到主窗口的那个事件应该可以解决问题。
You could also call mainwindow.overrideredirect(True) (section 24), which disables minimizing, resizing and closing via the buttons in the title bar.
您还可以调用 mainwindow.overrideredirect(True)(第 24 节),它通过标题栏中的按钮禁用最小化、调整大小和关闭。
回答by Bryan Oakley
The command you are looking for is wm_protocol, giving it "WM_DELETE_WINDOW"as the protocol to bind to. It lets you define a procedure to call when the window manager closes the window (which is what happens when you click the [x]).
您正在寻找的命令是wm_protocol,将其"WM_DELETE_WINDOW"作为要绑定的协议。它允许您定义在窗口管理器关闭窗口时要调用的过程(单击 时会发生这种情况[x])。
回答by The Demz
Using the method procotol, we can redefine the WM_DELETE_WINDOWprotocol by associating with it the call to a function, in this case the function is called on_exit:
使用该方法procotol,我们可以WM_DELETE_WINDOW通过将调用关联到一个函数来重新定义协议,在这种情况下,该函数被调用on_exit:
import tkinter as tk
from tkinter import messagebox
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Handling WM_DELETE_WINDOW protocol")
self.geometry("500x300+500+200")
self.make_topmost()
self.protocol("WM_DELETE_WINDOW", self.on_exit)
def on_exit(self):
"""When you click to exit, this function is called"""
if messagebox.askyesno("Exit", "Do you want to quit the application?"):
self.destroy()
def center(self):
"""Centers this Tk window"""
self.eval('tk::PlaceWindow %s center' % app.winfo_pathname(app.winfo_id()))
def make_topmost(self):
"""Makes this window the topmost window"""
self.lift()
self.attributes("-topmost", 1)
self.attributes("-topmost", 0)
if __name__ == '__main__':
App().mainloop()

