相关文章推荐
愤怒的风衣  ·  能否用JavaScript ...·  3 月前    · 
忧郁的薯片  ·  Tkinter sqlite insert ...·  3 月前    · 
不羁的肉夹馍  ·  使用MediaPipe和OpenCV的Pyt ...·  1 月前    · 
活泼的香槟  ·  江苏省泰州中学2024年公开招聘教师公告·  9 月前    · 
睿智的椰子  ·  android google tts ...·  2 年前    · 
害羞的足球  ·  会计专硕考研院校排名及考研难度分析 - 知乎·  2 年前    · 
刚分手的茶壶  ·  中本贯通详解,一位妈妈的经验之谈!让你提前被 ...·  2 年前    · 
Code  ›  如何处理Tkinter中的窗口关闭事件?开发者社区
root tkinter
https://cloud.tencent.com/developer/ask/sof/34460
爱笑的汉堡包
2 年前
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
提问
问 如何处理Tkinter中的窗口关闭事件?
Stack Overflow用户
提问于 2008-09-21 14:46:20
EN

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

8 200.2K 0 票数 163
EN
python
events
tkinter
window

回答 8

Stack Overflow用户

发布于 2008-09-21 14:51:11

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()
票数 235
EN

Stack Overflow用户

发布于 2018-04-13 01:50:02

根据Tkinter活动,尤其是在使用Tkinter.after时,使用 destroy() 停止此活动--即使使用协议()、按钮等--将干扰此活动(“执行时”错误),而不只是终止它。在几乎所有情况下,最好的解决方案都是使用标志。下面是一个关于如何使用它的简单、愚蠢的示例(尽管我确信大多数人不需要它!)

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 。

票数 17
EN

Stack Overflow用户

发布于 2019-10-20 08:10:36

我要感谢Apostolos的回答引起了我的注意。下面是2019年Python 3的一个更详细的示例,具有更清晰的描述和示例代码。

请注意,当用户关闭窗口时, destroy() (或根本没有自定义窗口关闭处理程序)将立即销毁窗口及其所有正在运行的回调。

这可能对你不好,这取决于你当前的Tkinter活动,特别是在使用 tkinter.after (定期回调)的时候。您可能正在使用一个回调函数来处理一些数据并将其写入磁盘...在这种情况下,您显然希望在不被突然终止的情况下完成数据写入。

最好的解决方案是使用标志。因此,当用户请求关闭窗口时,您将其标记为标志,然后对其做出反应。

(注意:我通常将GUI设计为封装得很好的类和独立的工作线程,而且我绝对不使用"global“(我使用类实例变量),但这是一个简单的简化示例,用于演示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
 
推荐文章
愤怒的风衣  ·  能否用JavaScript UI替换Python程序中的Tkinter UI?跨域问题与架构方案咨询
3 月前
忧郁的薯片  ·  Tkinter sqlite insert error - Python - The freeCodeCamp Forum
3 月前
不羁的肉夹馍  ·  使用MediaPipe和OpenCV的Python实现手势识别_手指数量识别python代码-CSDN博客
1 月前
活泼的香槟  ·  江苏省泰州中学2024年公开招聘教师公告
9 月前
睿智的椰子  ·  android google tts offline-掘金
2 年前
害羞的足球  ·  会计专硕考研院校排名及考研难度分析 - 知乎
2 年前
刚分手的茶壶  ·  中本贯通详解,一位妈妈的经验之谈!让你提前被“预录取”,大学我来了!_专业
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号