Intercept Tkinter "Exit" command? Intercept Tkinter "Exit" command? tkinter tkinter

Intercept Tkinter "Exit" command?


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)


You can use python atexit module.

For example:

import atexitdef doSomethingOnExit():    passatexit.register(doSomethingOnExit)


In my case, the following code didn't work:

root.protocol("WM_DELETE_WINDOW", app.on_delete)  # doesn't work

However, it worked using this form:

root.wm_protocol ("WM_DELETE_WINDOW", app.on_delete)  # does work