Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept Tkinter "Exit" command?

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?

like image 521
John Avatar asked Jan 10 '11 01:01

John


2 Answers

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.

Usage:

root.protocol("WM_DELETE_WINDOW", app.on_delete)
like image 118
Bryan Oakley Avatar answered Oct 04 '22 14:10

Bryan Oakley


You can use python atexit module.

For example:

import atexit

def doSomethingOnExit():
    pass

atexit.register(doSomethingOnExit)
like image 42
Maksym Ganenko Avatar answered Oct 04 '22 14:10

Maksym Ganenko