拦截Tkinter“退出"命令? [英] Intercept Tkinter "Exit" command?

查看:19
本文介绍了拦截Tkinter“退出"命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Tkinter 用 Python 编写客户端-服务器程序.我需要服务器来跟踪连接的客户端.为此,我想让客户端在单击退出按钮(角落的标准X")后向服务器发送一条自动消息.我如何知道用户何时退出程序?

I'm writing a client-server program in Python with Tkinter. I need the server to keep track of the connected clients. For this, I would like to have the client send an automated message to the server after the exit button(the standard "X" in the corner) is clicked. How can I know when the user is exiting the program?

推荐答案

您想使用 wm_protocol 顶层窗口的方法.具体来说,您对 WM_DELETE_WINDOW 协议感兴趣.如果您使用该方法,它允许您注册一个回调,该回调在窗口被销毁时调用.

You want to use the wm_protocol method of the toplevel window. Specifically, you are interested in the WM_DELETE_WINDOW protocol. If you use that method, it allows you to register a callback which is called when the window is being destroyed.

用法:

root.protocol("WM_DELETE_WINDOW", app.on_delete)

这篇关于拦截Tkinter“退出"命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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