[Tkinter-discuss] Doing things on Tkinter termination

Geoff Bache geoff.bache at gmail.com
Tue Jan 26 10:11:31 CET 2010


Hi all,

How would you add some code that is called when the application is
terminated, that is independent of whether it was closed via the
window manager or via some programmatic call within the application,
such as a call from a "quit" button?

I tried binding to the "destroy" event like this:

from Tkinter import *

def destroyed(*args):
    print "AAARRGGGH!!!"

root = Tk()

button = Button(root, text="QUIT", command=root.quit)
button.grid()

# This line seems to lead to Python crashes on Windows
root.bind("<Destroy>", destroyed, "+")
root.mainloop()

but this produces Python crashes on Windows, and doesn't work on Linux
either if the Quit button is pressed. Changing to
"command=root.destroy" works
on Linux but produces a hang on Windows.

There must be a better way, surely?

Regards,
Geoff


More information about the Tkinter-discuss mailing list