[Tkinter-discuss] Problems with lambda functions in callbacks

Stewart Midwinter stewart at midwinter.ca
Thu Feb 15 18:26:20 CET 2007

Dídac, aquest és un problem molt interessant.

Your code doesn't work because you didn't pass in a reference to i in your
lambda.  The correct code appears below. BTW, if you set up your application
with classes and methods, you will need to pass in a reference to self in
the same way: self=self.

from Tkinter import *

root = Tk();

def cb(x):
   print x;

for i in range(5):
   b = Button(root,text=i,command=lambda i=i:cb(i));


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20070215/04841033/attachment.htm 

More information about the Tkinter-discuss mailing list