Detect TKinter window being closed?

Adonis adonisv at
Fri Dec 2 11:19:39 EST 2005

Glen wrote:
> Is it possible to to detect a Tkinter top-level window being closed with the
> close icon/button (top right), for example to call a function before the
> window actually closes?
> Python 2.4 / Linux (2.6 kernel) if that makes any difference.
> Any info would be greatly appreciated.
> Thanks
> Glen

Here is an example code taken from:
(located at very end)

Example 7-2. Capturing destroy events

# File:

from Tkinter import *
import tkMessageBox

def callback():
     if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):

root = Tk()
root.protocol("WM_DELETE_WINDOW", callback)


Hope this helps.


More information about the Python-list mailing list