[Tkinter-discuss] Can't do proper event binding in a for loop
Michael Lange
klappnase at web.de
Tue Oct 19 13:24:34 CEST 2010
Hi,
Thus spoketh Firat Ozgul <ozgulfirat at gmail.com>
unto us on Tue, 19 Oct 2010 13:19:03 +0300:
> Hello,
>
> for loop doesn't work, because in a for loop all events will be bound
> all at once, and you will only see the effect of the last binding. You
> need something that binds events one by one.
>
> If I were you, I would use the next() method of Python iterators:
I think the OP suffered from a different problem in her code, namely the
IndexError that will be raised when i+1 or i-1 become >2 or <0 .
I guess what she wanted to achieve can be done, but not with this simple
lambda, she needs to catch the IndexError with something like:
def up_and_down(*buttons):
def callback(event):
index = list(buttons).index(event.widget)
try:
if event.keysym == 'Up':
buttons[index - 1].focus_set()
elif event.keysym == 'Down':
buttons[index + 1].focus_set()
except IndexError:
pass
for i in range(len(buttons)-1):
buttons[i].bind("<Down>", callback)
for i in range(1, len(buttons)):
buttons[i].bind("<Up>", callback)
I hope this helps
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
Wait! You have not been prepared!
-- Mr. Atoz, "Tomorrow is Yesterday", stardate 3113.2
More information about the Tkinter-discuss
mailing list