I want to make 5 buttons and each buttons bind a commend to print this index. but always print 5.
My code like this,
for i in range(5):
make_button = Tkinter.Button(frame, text ="make!", command= lambda: makeId(i))
It always print 5 , then how can i do ? thanks a lot~~
Best How To :
Resolution of variables in lambdas is done when lambda is executed. At this time, for all buttons i=5. To rectify this issue do as follows:
make_button = Tkinter.Button(frame, text ="make!", command= lambda i=i: makeId(i))
This creates i as a local variable in a lambda. This local variable will hold correct value of i from the loop. the local variable can have any name, not necessarily
command= lambda a=i: makeId(a)).