Tkinter unbinding

Peter Otten __peter__ at web.de
Fri Dec 19 16:14:21 CET 2008


Roger wrote:

>> either. I'd suggest a plain-python workaround along the lines of
> 
> Wow.  You just blew my mind.  I'm going to play with this.  Thanks a
> lot, I've really learned a lot in just that small bit.  I don't have
> much experience in playing with a lot of the 'private' calls such as
> __call__.  I need to do some more reading.

Here's a non-OO variant:

funcs = []
def call(event):
    for f in list(funcs): # *
        f(event)
root.bind("<1>", call)

funcs.append(test)
funcs.append(test2)
root.bind("<1>", call)

def unbind():
    print "unbind"
    funcs.remove(test2)

I does the same, but is a bit harder to manage if you have more than one
event/widget to deal with.

(*) iterating over a copy of the list of functions is slightly more robust
as it will not accidentally skip callbacks when the original list is
modified during iteration. I suggest that you change the OO version
accordingly.

Peter



More information about the Python-list mailing list