469,647 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

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

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?
Jul 19 '05 #1
4 4424
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)

iD8DBQFCq5suJd01MZaTXX0RAjCsAKCopeOFSbNIatjYupztNl ln57j6UwCdHquu
+QmRYrRlpBvfP0/5w9GZ3E4=
=7+oL
-----END PGP SIGNATURE-----

Jul 19 '05 #2
je****@unpythonic.net wrote:
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]
Jul 19 '05 #3
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-----

Jul 19 '05 #4
Jeff Epler wrote:
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! :)
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Gregory | last post: by
2 posts views Thread by Rob | last post: by
3 posts views Thread by Mickel Grönroos | last post: by
2 posts views Thread by Russell E. Owen | last post: by
6 posts views Thread by max(01)* | last post: by
1 post views Thread by C D Wood | last post: by
reply views Thread by John Hunter | last post: by
3 posts views Thread by J-Burns | last post: by
reply views Thread by nicstel | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.