tkinter bind function with variable in a loop [duplicate] tkinter bind function with variable in a loop [duplicate] tkinter tkinter

tkinter bind function with variable in a loop [duplicate]


Your problem here is that the variable i gets captured by the lambda, but you can get around that by creating a little helper function for example:

for i in range(2):    def make_lambda(x):        return lambda ev:SomeFunc(ev,x)    root.bind("<KeyPress-%c>" % keys[i], make_lambda(i))

This creates a new scope for each binding you create, thus executing the for loop and changing of i during the loop does not influence your already lambda functions.