-1
我有以下一段代码,我在其中创建一系列按钮以在创建的数字类上运行增加或减少方法。按预期在列表中选定的对象,但我的问题越来越按钮到正确的对象进行操作TKinter使用索引for循环来传递按钮命令的参数
for idx in range(len(self._priceNums)):
Button(window, text='^', command=lambda: self.incNum(idx)).grid(row=0, column=numInColumn)
Label(window, textvariable=self._priceNums[idx]).grid(row=1, column=numInColumn)
Button(window, text='v', command=lambda: self.decNum(idx)).grid(row=2, column=numInColumn)
numInColumn += 1
的功能工作。
lamba: self.incNum(idx)
似乎只在按下按钮时才读取idx。因此每个按钮只能在列表中最后一个对象上运行该方法。有没有办法以这种方式创建一系列按钮,每个按钮都对应于此列表中的相应数字对象。下面是该方法以供参考
def incNum(self, idx):
self._priceNums[idx].set(self.game._adjustedPrice[idx].incNum())
注意,可能会或可能不会有所帮助:
_priceNums对应的值StringVars列表的数量_adjustedPrice
对象,谢谢!