To detect when the user attempts to close a window, you need to use the
wm protocol command to detect a "WM_DELETE_WINDOW" message from the window manager. Here's how you do it:
Code:
wm protocol . WM_DELETE_WINDOW {
# Do whatever you like here
}
Of course, you can replace "." with the name of another toplevel window if your application has multiple toplevels.
As the comment above indicates, you can do anything you like in your WM_DELETE_WINDOW protocol handler, such as save your application's state in a data file, prompt the user if he or she really wants to quit, etc. In practice, if my application already has a "Quit" button or menu entry, usually I'll have my protocol handler simply invoke that button or menu entry. For example:
Code:
wm protocol . WM_DELETE_WINDOW {
.mbar.file invoke "Quit"
}
The default WM_DELETE_WINDOW protocol handler simply destroys the toplevel (which exits your application, if it's your main window, "."

. But if you override the default handler, then you don't have to destroy the window if you don't want to. In fact, the following code prevents a user from closing your window using any of the standard window manager techniques:
Code:
# In the following line, you must have at
# least one whitespace character between the
# { }. If you have no characters in the
# argument (an empty string), the wm protocol
# command interprets it as "restore the
# default protocol handler."
wm protocol . WM_DELETE_WINDOW { }
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax