拦截Tkinter“退出"命令? [英] Intercept Tkinter "Exit" command?
问题描述
我正在用 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屋!