Reputation: 17
Using Tkinter in python, trying to make numbered buttons, which use self.do(x) to add the number x to a string variable. The problem with this particular piece of code being in a loop (to save space), is that it will add the LAST number to the string (ie, 9 in this example). This is because it calls the function after this, and uses the latest value of num[i]. Is there any way to correct this?
self.numButton = []
num = []
for i in range(9):
num.append(i + 1)
self.numButton.append(Button(root,text=num[i],command=lambda: self.do(num[i])))
Upvotes: 1
Views: 111
Reputation: 879103
Use a default value in your lambda
function:
self.numButton.append(
Button(root,text=num[i],command=lambda i=i: self.do(num[i])))
The default value is evaluated and bound to the function at the time the lambda
function is defined (as opposed to when it is run). So, later, when the button is pressed and the callback is called without any arguments, the default value is used.
Since a different default value for i
is bound to each lambda
function, the appropriate value for i
is used for each callback.
If the callback requires additional arguments, such as on event
, just place the parameter with default value at the end. For example,
root.bind('Key-{n}'.format(n=num[i]), lambda e, i=i: self.do(num[i]))
Upvotes: 1