We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Tkinter: Catching Window Deletion

March 28, 1998 | Fredrik Lundh

To handle deletion of top level windows, use the protocol method to attach a deletion handler to the window. The deletion handler can do anything it wants, including not destroying the window (possibly after displaying an “are you sure you want to quit” message box).

from Tkinter import *

root = Tk()

def _delete_window():
    print "delete_window"
    try:
	root.destroy()
    except:
	pass

def _destroy(event):
    print "destroy"

root.protocol("WM_DELETE_WINDOW", _delete_window)
root.bind("<Destroy>", _destroy)

button = Button(root, text="Destroy", command=root.destroy)
button.pack()

mainloop()