My problem with the following code is passing 'i' (just a simple range of numbers but changes according to number_boxes) through lambda to callback in order to have seperate functionality of each box created.
I have tried reading tutorials and attempted various things in my code but it either doesn't work or I get errors 'lambda() requires only 2 arguments, 3 given' etc. I believe I would need to make 'i' a list but I still get this particular error..
I have commented on the code where the problems arise. I need to return the values inside each box as well as overwrite the text.
Thank you.
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
UPDATE: Current problem: Need to pass all values of 'i' to callback and not just one but how to put list into lambda?
Error:
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)
po = box[i].get()
: shouldn't it bepo = self.box[i].get()
instead ? I don't understandlambda i: self.numberwritten(n)
either: where doesn
come from ? – Emmanuel Jul 5 '12 at 14:14