[Tkinter-discuss] Can't do proper event binding in a for loop

Michael Lange klappnase at web.de
Tue Oct 19 22:15:53 CEST 2010


Thus spoketh "Michael O'Donnell" <michael.odonnell at uam.es> 
unto us on Tue, 19 Oct 2010 21:43:57 +0200:

> Much simpler, and closer to the original posters intention.
> Replace your up_and_down function with:
> 
> def up_and_down(*buttons):
> 
>   for i in range(len(buttons)-1):
>     buttons[i].bind("<Down>", lambda e, x=buttons[i+1]: x.focus_set())
> 
>   for i in range(1, len(buttons)):
>     buttons[i].bind("<Up>", lambda e, x=buttons[i-1]: x.focus_set())
> 

Sure, you're right.
Here's another, even simpler, candidate:

def up_and_down(*buttons):
    for b in buttons[:-1]:
        b.bind('<Down>', lambda event: b.event_generate('<Tab>'))
    for b in buttons[1:]:
        b.bind('<Up>', lambda event: b.event_generate('<Shift-Tab>'))

Regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

I'm frequently appalled by the low regard you Earthmen have for life.
		-- Spock, "The Galileo Seven", stardate 2822.3


More information about the Tkinter-discuss mailing list