我对以下代码的问题是通过 lambda 将“i”(只是一个简单的数字范围,但根据 number_boxes 更改)传递给回调,以便创建每个盒子的单独功能。
我已经尝试阅读教程并在我的代码中尝试了各种事情,但它要么不起作用,要么我得到错误'lambda()只需要 2 个参数,3 个给定'等。我相信我需要制作 'i' 一个列表但是我仍然得到这个特殊的错误..
我已经评论了出现问题的代码。我需要返回每个框中的值并覆盖文本。
谢谢你。
self.clicked = [] # In my __init__ definition
self.numbers = [StringVar() for i in xrange(self.number_boxes) ] # Create Stringvar for each box
for i in xrange(self.number_boxes): # For each number, create a box
self.clicked.append(False) # Not clicked in yet
self.box.append(Entry(self.frame_table,bg='white',borderwidth=0, width=10, justify="center", textvariable=self.numbers[i], fg='grey')) # Textvariable where I enter a value to get after, need to do for each box (use box[i] but cannot for append)
self.box[i].grid(row=row_list,column=column+i, sticky='nsew', padx=1, pady=1)
self.box[i].insert(0, "Value %g" % float(i+1))
self.box[i].bind("<Button-1>", lambda event, index=i : self.callback(event, index)) # Need to pass the 'i's' to callback but having lambda difficulties
for i in self.numbers:
i.trace('w',lambda index=i: self.numberwritten(index) ) # Need for each box again here
def numberwritten(self, index): # A way of combining numberwritten and callback?
po = self.box[index].get() # Get the values in each box
print po
def callback(self, event, index):
if (self.clicked[index] == False): # When clicked in box, overwrite default text with value and change colour to black, not grey
self.box[index].delete(0, END)
self.box[index].config(fg='black')
self.clicked[index] = True
更新:当前问题:需要将“i”的所有值传递给回调,而不仅仅是一个,而是如何将列表放入 lambda?
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
TypeError: lambda() takes at most 1 argument (3 given)