Tkinter unbinding

Bad Mutha Hubbard badmuthahubbard at usenet.cnntp.org
Fri Dec 19 09:46:00 CET 2008


Roger wrote:

> I've done a lot of googling for this topic and I fear that it's not
> possible.  I have a widget that is overloaded with several bindings.
> I want to be able to unbind one method form the same Event without
> destroying all the other bindings to the same event that's associated
> to the same widget.
>
> For example:
>
> import Tkinter
>
> def test():
>         print 'test'
>
> def test2():
>         print 'test2'
>
> root = Tkinter.Tk()
> funcid1 = root.bind("<1>", lambda e: test())
> funcid2 = root.bind("<1>", lambda e: test2(), add='+')
> root.unbind("<1>", funcid2)
> root.mainloop()
>
> When run neither <1> binding will exist against the root because the
> unbind will unbind all the functions associated with that event.
> However, in this example, I only want to unbind test2 not test1.
>
> Any help is greatly appreciated.  Thanks!
> Roger.

I believe you've discovered a bug.  Aside from recommending trying
wxWidgets, here's the source of the unbind function in Tkinter.py:

    def unbind(self, sequence, funcid=None):
        """Unbind for this widget for event SEQUENCE  the
        function identified with FUNCID."""
        self.tk.call('bind', self._w, sequence, '')
        if funcid:
            self.deletecommand(funcid)

-------------------------------------------
First, it replaces all bindings for the sequence with the empty string,
i.e., it deletes all bindings for that event unconditionally.  THEN it
calls deletecommand() with the funcid, who knows what that does.  My Tcl
is not so sharp.
I have an idea for a workaround, let me see if it works...
-Chuckk




More information about the Python-list mailing list