Detect TKinter window being closed?

Adonis adonisv at DELETETHISTEXTearthlink.net
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:
http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
(located at very end)

Example 7-2. Capturing destroy events

# File: protocol1.py

from Tkinter import *
import tkMessageBox

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

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

root.mainloop()

Hope this helps.

Adonis



More information about the Python-list mailing list