Capture close window button in Tkinter

W

William Gill

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'? Also, how do I capture
the root window close button?

Thanks,

Bill
 
J

Jonathan Ellis

William said:
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)
 
W

William Gill

Jonathan said:
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?
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
events.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top