如何处理 Tkinter 中的窗口关闭事件?
- 2025-01-15 08:45:00
- admin 原创
- 86
问题描述:
如何在 Python Tkinter 程序中处理窗口关闭事件(用户单击“X”按钮)?
解决方案 1:
Tkinter 支持一种称为协议处理程序的机制。此处,术语协议是指应用程序与窗口管理器之间的交互。最常用的协议称为WM_DELETE_WINDOW
,用于定义当用户使用窗口管理器明确关闭窗口时会发生什么。
您可以使用该protocol
方法为该协议安装处理程序(小部件必须是Tk
或Toplevel
小部件):
这里有一个具体的例子:
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
解决方案 2:
Matt 展示了关闭按钮的一个经典修改。
另一个是让关闭按钮最小化窗口。
您可以通过将iconify方法
作为协议方法的第二个参数来重现此行为。
这是一个在 Windows 7 和 Windows 10 上测试过的工作示例:
# Python 3
import tkinter
import tkinter.scrolledtext as scrolledtext
root = tkinter.Tk()
# make the top right close button minimize (iconify) the main window
root.protocol("WM_DELETE_WINDOW", root.iconify)
# make Esc exit the program
root.bind('<Escape>', lambda e: root.destroy())
# create a menu bar with an Exit command
menubar = tkinter.Menu(root)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
# create a Text widget with a Scrollbar attached
txt = scrolledtext.ScrolledText(root, undo=True)
txt['font'] = ('consolas', '12')
txt.pack(expand=True, fill='both')
root.mainloop()
在这个例子中,我们为用户提供了两个新的退出选项:
经典的文件→退出,以及Esc
按钮。
解决方案 3:
如果您想改变 x 按钮的功能或使其根本无法关闭,请尝试此操作。
yourwindow.protocol("WM_DELETE_WINDOW", whatever)
然后定义“无论什么”的含义
def whatever():
# Replace this with your own event for example:
print("oi don't press that button")
您还可以这样做,当您关闭该窗口时,您可以像这样回调它
yourwindow.withdraw()
这会隐藏窗口,但不会关闭它
yourwindow.deiconify()
这使得窗口再次可见
解决方案 4:
根据 Tkinter 活动,尤其是在使用 Tkinter.after 时,使用destroy()
(即使使用 protocol()、按钮等)停止此活动也会干扰此活动(“执行时”错误)而不是直接终止它。几乎每种情况下的最佳解决方案都是使用标志。这是一个简单、愚蠢的如何使用它的示例(尽管我确信你们大多数人都不需要它!:)
from Tkinter import *
def close_window():
global running
running = False # turn off while loop
print( "Window closed")
root = Tk()
root.protocol("WM_DELETE_WINDOW", close_window)
cv = Canvas(root, width=200, height=200)
cv.pack()
running = True;
# This is an endless loop stopped only by setting 'running' to 'False'
while running:
for i in range(200):
if not running:
break
cv.create_oval(i, i, i+1, i+1)
root.update()
这样就可以完美地终止图形活动。您只需要running
在正确的位置进行检查即可。
解决方案 5:
我要感谢 Apostolos 的回答让我注意到了这一点。这是 2019 年 Python 3 的更详细示例,其中包含更清晰的描述和示例代码。
请注意,当用户关闭窗口时destroy()
(或者根本没有自定义窗口关闭处理程序),窗口及其所有正在运行的回调将会立即被销毁。
这可能对您不利,具体取决于您当前的 Tkinter 活动,尤其是在使用tkinter.after
(定期回调)时。您可能正在使用处理某些数据并写入磁盘的回调……在这种情况下,您显然希望数据写入完成而不会被突然终止。
最好的解决方案是使用标志。因此,当用户请求关闭窗口时,您可以将其标记为标志,然后对其做出反应。
(注意:我通常将 GUI 设计为封装良好的类和单独的工作线程,而且我绝对不使用“全局”(我使用类实例变量),但这是一个简单、精简的示例,用于演示 Tk 如何在用户关闭窗口时突然终止您的定期回调...)
from tkinter import *
import time
# Try setting this to False and look at the printed numbers (1 to 10)
# during the work-loop, if you close the window while the periodic_call
# worker is busy working (printing). It will abruptly end the numbers,
# and kill the periodic callback! That's why you should design most
# applications with a safe closing callback as described in this demo.
safe_closing = True
# ---------
busy_processing = False
close_requested = False
def close_window():
global close_requested
close_requested = True
print("User requested close at:", time.time(), "Was busy processing:", busy_processing)
root = Tk()
if safe_closing:
root.protocol("WM_DELETE_WINDOW", close_window)
lbl = Label(root)
lbl.pack()
def periodic_call():
global busy_processing
if not close_requested:
busy_processing = True
for i in range(10):
print((i+1), "of 10")
time.sleep(0.2)
lbl["text"] = str(time.time()) # Will error if force-closed.
root.update() # Force redrawing since we change label multiple times in a row.
busy_processing = False
root.after(500, periodic_call)
else:
print("Destroying GUI at:", time.time())
try: # "destroy()" can throw, so you should wrap it like this.
root.destroy()
except:
# NOTE: In most code, you'll wanna force a close here via
# "exit" if the window failed to destroy. Just ensure that
# you have no code after your `mainloop()` call (at the
# bottom of this file), since the exit call will cause the
# process to terminate immediately without running any more
# code. Of course, you should NEVER have code after your
# `mainloop()` call in well-designed code anyway...
# exit(0)
pass
root.after_idle(periodic_call)
root.mainloop()
此代码将向您显示,WM_DELETE_WINDOW
即使我们的自定义程序periodic_call()
在工作/循环中间忙碌,处理程序也会运行!
我们使用了一些相当夸张的.after()
值:500 毫秒。这只是为了让您非常容易地看到在定期呼叫忙时关闭与不关闭之间的区别……如果您在数字更新时关闭,您将看到在您的定期呼叫“忙于处理:真”时WM_DELETE_WINDOW
发生了这种情况。如果您在数字暂停时关闭(意味着定期回调当时没有处理),您会看到关闭发生在它“不忙”时。
在实际使用中,您.after()
需要花费大约 30-100 毫秒的时间才能获得响应式 GUI。这只是一个演示,旨在帮助您了解如何保护自己免受 Tk 默认的“关闭时立即中断所有工作”行为的影响。
总之:让WM_DELETE_WINDOW
处理程序设置一个标志,然后.destroy()
在安全时(当您的应用程序完成所有工作时)定期并手动检查窗口的标志。
PS:您还可以WM_DELETE_WINDOW
询问用户是否真的想关闭窗口;如果他们回答“不”,则无需设置标志。这非常简单。您只需在您的窗口中显示一个消息框WM_DELETE_WINDOW
并根据用户的回答设置标志即可。
解决方案 6:
您应该使用 destroy() 来关闭 tkinter 窗口。
from Tkinter import *
root = Tk()
Button(root, text="Quit", command=root.destroy).pack()
root.mainloop()
解释:
root.quit()
如果执行命令,
上面这一行只是绕过了root.mainloop()
ie,它root.mainloop()
仍将在后台运行。quit()
root.destroy()
当destroy()
命令消失时root.mainloop()
即root.mainloop()
停止。
因此,当您只想退出程序时,您应该使用root.destroy()
它来停止 mainloop()。
但是如果你想要运行一些无限循环,并且你不想破坏你的 Tk 窗口,并且想root.mainloop()
在行后执行一些代码,那么你应该使用root.quit()
。例如:
from Tkinter import *
def quit():
global root
root.quit()
root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something
解决方案 7:
最简单的代码是:
from tkinter import *
window = Tk()
隐藏窗口:window.withdraw()
显示窗口:window.deiconify()
退出窗口:exit()
退出窗口(如果您已经创建了.exe文件):
from tkinter import *
import sys
window = Tk()
sys.exit()
当然,你必须放置一个按钮并在函数中使用上面的代码,这样你就可以在按钮的命令部分输入函数的名称
解决方案 8:
你可以使用:
root = Tk()
def func():
print('not clossed')
root.protocol('wm_delete_window', func)
root.mainloop()
解决方案 9:
def on_closing():
if messagebox.askokcancel("Quit", "would you like to quit"):
window.destroy()
window.protocol("WM_DELETE_WINDOW", on_closing)
您可以像这样处理窗口关闭事件,如果您想做其他事情,只需改变 on_closing() 函数中发生的事情。
解决方案 10:
尝试简单版本:
import tkinter
window = Tk()
closebutton = Button(window, text='X', command=window.destroy)
closebutton.pack()
window.mainloop()
或者如果你想添加更多命令:
import tkinter
window = Tk()
def close():
window.destroy()
#More Functions
closebutton = Button(window, text='X', command=close)
closebutton.pack()
window.mainloop()
解决方案 11:
from tkinter import *
w = Tk()
def c():
global w
print('Window closing')
w.destroy()
w.protocol('WM_DELETE_WINDOW', c)
解决方案 12:
我认为更简单的方法是使用break
命令,例如
import tkinter as tk
win=tk.Tk
def exit():
break
btn= tk.Button(win, text="press to exit", command=exit)
win.mainloop()
或使用sys.exit()
import tkinter as tk
import sys
win=tk.Tk
def exit():
sys.exit
btn= tk.Button(win, text="press to exit", command=exit)
win.mainloop()