I am writing tkinter GUI. I use list to store my tkinter button instance.
Like this:
self.vec=[]
for i in range(25) :
self.vec.append( Button(self.gframe) )
self.vec[i]["width"] = 3
self.vec[i]["command"] = lambda : self.placeChess(i)
I hope
the command of button0
will be lambda : self.placeChess(0)
.
The command of button1
will be lambda : self.placeChess(1)
The command of button2
will be lambda : self.placeChess(2)
... and so on
But the code I write above doesn't do that. No matter what button I click, the parameter of placeChess
always is 25, and 25 is the final value of i
. It seems that python pass reference of i
to placeChess
, not the constant value every time lambda
created.
How to solve this problem?