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

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

问题描述

我在Python中使用Tkinter编写了一个客户端 - 服务器程序。我需要服务器来跟踪连接的客户端。为此,我希望客户端在点击退出按钮(角落中的标准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 方法的toplevel窗口。具体来说,您对 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天全站免登陆