Capture close window button in Tkinter

Jonathan Ellis jbellis at
Sun Jun 12 18:35:16 CEST 2005

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.

> Also, how do I capture
> the root window close button?

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

More information about the Python-list mailing list