python 关闭窗口事件_关于python:如何在Tkinter中处理窗口关闭事件?

如何在Python Tkinter程序中处理窗口关闭事件(用户单击" X"按钮)?

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()

我使用了类似的代码,但root.destroy()

如果您使用的是像Twisted这样的东西来独立维护事件循环或Tkinter(例如:twists反应堆对象),请确保外部主循环已停止,并且为此目的提供了任何专门的技术(例如:twisted.reactor.stop())

在Windows上的Python 2.7上,Tkinter没有子模块消息框。 我使用了import tkMessageBox as messagebox

我认为您应该知道是您从其他人/其他地方复制了此答案和代码。

我不知道,那不是我最初发布的代码。

不为我工作。 硬关闭窗口时(例如使用Alt + F4),它不会改变经典的Python对图形中断的混乱反应。

@MitchMcMabers:另一方面,很长一段时间以来,tkinter几乎没有任何重大变化。

马特展示了关闭按钮的一种经典修改。

另一种是使关闭按钮最小化窗口。

您可以通过使用iconify方法来重现此行为

是协议方法的第二个参数。

这是一个在Windows 7上测试过的有效示例:

# Python 3

import tkinter

import tkinter.scrolledtext as scrolledtext

class GUI(object):

def __init__(self):

root = self.root = tkinter.Tk()

root.title('Test')

# 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('', 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')

gui = GUI()

gui.root.mainloop()

在此示例中,我们为用户提供了两个新的退出选项:

经典文件菜单->退出,还有Esc按钮。

取决于Tkinter活动,尤其是在使用Tkinter.after时,使用destroy()停止此活动-即使使用protocol(),按钮等-也会干扰此活动("执行时出错"),而不是只是终止它。在几乎每种情况下,最好的解决方案是使用标志。这是一个简单,愚蠢的用法示例(尽管我敢肯定,你们大多数人都不需要它!

from Tkinter import *

def close_window():

global running

running = False

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。

尝试简单版本:

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()

我要感谢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在定期呼叫时发生呼叫"正在处理:True"。如果您在数字暂停时关闭(这意味着此时不处理定期回调),则会看到关闭发生在"不忙"期间。

在实际使用中,您的.after()将使用30到100毫秒之类的时间来具有响应GUI。这只是一个演示,可以帮助您了解如何保护自己免受Tk的默认"关闭时立即中断所有工作"的行为。

总结:使WM_DELETE_WINDOW处理程序设置一个标志,然后在安全的情况下(当您的应用程序完成所有工作时)定期并手动检查该标志.destroy()窗口。

PS:您也可以使用WM_DELETE_WINDOW询问用户是否真的要关闭窗口;如果他们回答"否",则无需设置标志。非常简单您只需在WM_DELETE_WINDOW中显示一个消息框,然后根据用户的答案设置标志即可。

使用closeEvent

def closeEvent(self, event):

# code to be executed

这个答案需要更多细节。 这条线放在哪里? 在我这边摆弄一点似乎无法使它正常工作。

你可能感兴趣的:(python,关闭窗口事件)