TKinter -- '<Destroy>' event executing more than once?

C

Christopher Subich

I'm building an application involving both twisted and Tkinter. Since
twisted co-opts <widget>.mainloop() in its reactor.run(), and since it
behaves very badly if the application quits without reactor.stop()
running, I attach the following function to '<Destroy>' in the main
window (root = Tk()):

def stop_reactor_bind(x):
reactor.stop()

Then:
root.bind('<Destroy>',stop_reactor_bind)

The problem, however, comes that when I add a Text widget inside the
root window, upon destroying the window (closing it) the callback seems
to execute twice. In interactive testing, it's executed once per widget
inside the root window. Since twisted doesn't take multiple
reactor.stop()s gracefully, how (short of wrapping this inside a
class/scope that keeps state) can I ensure that the callback executes
only once? Am I attaching to the wrong signal?
 
J

jepler

Bindings created on a Toplevel or Tk widget apply to *all* widgets in the same
toplevel.

So you're seeing a <Destroy> event for each widget you create...

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCq5suJd01MZaTXX0RAjCsAKCopeOFSbNIatjYupztNlln57j6UwCdHquu
+QmRYrRlpBvfP0/5w9GZ3E4=
=7+oL
-----END PGP SIGNATURE-----
 
C

Christopher Subich

Bindings created on a Toplevel or Tk widget apply to *all* widgets in
the same toplevel. So you're seeing a <Destroy> event for each widget
you create...

Oh. :) Is there a way of binding the event just to the window itself,
or should I just check that the widget referenced in the 'event'?
[update: not even sure how I'd do this anyway, since the widget returned
in the event "is not" the root widget]
 
J

Jeff Epler

For me, an 'is' test works to find out what widget the event is taking
place on.

#------------------------------------------------------------------------
import Tkinter

def display_event(e):
print "event received", e.widget, e.widget is t

t = Tkinter.Tk()
t.bind("<Destroy>", display_event)
w = Tkinter.Entry(t)
t.destroy()
#------------------------------------------------------------------------

This program prints:
event received .-1209415348 False
event received . True

if that fails, you could compare str(e.widget) and t._w, though this can
give a false positive if you have multiple Tk() instances---each Tk()
instance is called ".".

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCrDc0Jd01MZaTXX0RAt/vAJwPoBlfbHEXuF4ppeFVyDiWuI71FwCeJc/K
wIY45XQ2F9mtqfCH+cR/oC4=
=MHOq
-----END PGP SIGNATURE-----
 
C

Christopher Subich

Jeff said:
For me, an 'is' test works to find out what widget the event is taking
place on.

.... yes, I am apparently as stupid as I look. In my test code, I was
trying "if event is widget," and I just now saw that. Thanks! :)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top