Capture close window button in Tkinter

William Gill noreply at
Sun Jun 12 20:17:05 CEST 2005

Jonathan Ellis wrote:
> William Gill wrote:
>>I am trying to make a simple data editor in Tkinter where each data
>>element has a corresponding Entry widget.   I have tried to use the
>>FocusIn/FocusOut events to set a 'hasChanged' flag  (if a record has not
>>changed, the db doesn't need updating).  This seems to work fine except
>>that when the user finishes and clicks a 'done' button or the close
>>window button (in the root widget) no FocusOut event is triggered.  I
>>can trigger a FocusOut event if the 'done' button opens another window
>>(i.e. a messagebox) that takes focus.  Enter and Leave follow the mouse,
>>but don't trigger when the user tabs between fields.
>>Is there a better way to monitor 'hasChanged'?
> I'd go with monitoring keypresses in the Entry widget.

Well, It doesn't seem logical to do a before and after test of the Entry 
values after each keypress, but I suppose I could monitor keypresses, 
test event.key for Tab (in case the user tabbed out of the widget w/o 
making any changes.

Is that along the lines of what you are suggesting?

>>Also, how do I capture
>>the root window close button?
>     root = Tk()
>     root.protocol("WM_DELETE_WINDOW", onexit)
I had seen something like this somewhere, but couldn't remember where. 
Thanks to your reminder I found an example of "Capturing destroy 
events."  Since that will allow me to route the exit process through a 
tkMessageBox, it may be just as good to continue using FocusIn/FocusOut 

More information about the Python-list mailing list