By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,403 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,403 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.