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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Subich

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

    I'm building an application involving both twisted and Tkinter. Since
    twisted co-opts <widget>.mainlo op() 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_bi nd(x):
    reactor.stop()

    Then:
    root.bind('<Des troy>',stop_rea ctor_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?
  • jepler@unpythonic.net

    #2
    Re: TKinter -- '&lt;Destroy&gt ;' event executing more than once?

    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)

    iD8DBQFCq5suJd0 1MZaTXX0RAjCsAK CopeOFSbNIatjYu pztNlln57j6UwCd Hquu
    +QmRYrRlpBvfP0/5w9GZ3E4=
    =7+oL
    -----END PGP SIGNATURE-----

    Comment

    • Christopher Subich

      #3
      Re: TKinter -- '&lt;Destroy&gt ;' event executing more than once?

      jepler@unpython ic.net wrote:
      [color=blue]
      > 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...[/color]

      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]

      Comment

      • Jeff Epler

        #4
        Re: TKinter -- '&lt;Destroy&gt ;' event executing more than once?

        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("<Destro y>", 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)

        iD8DBQFCrDc0Jd0 1MZaTXX0RAt/vAJwPoBlfbHEXuF 4ppeFVyDiWuI71F wCeJc/K
        wIY45XQ2F9mtqfC H+cR/oC4=
        =MHOq
        -----END PGP SIGNATURE-----

        Comment

        • Christopher Subich

          #5
          Re: TKinter -- '&lt;Destroy&gt ;' event executing more than once?

          Jeff Epler wrote:[color=blue]
          > For me, an 'is' test works to find out what widget the event is taking
          > place on.[/color]

          .... 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! :)

          Comment

          Working...