-1

I'm having a problem where I create a for loop of buttons and I want each one of other to have different column, row and text. I noticed only the text and the column (because i used lambda on it) are actually the updated values given to the function. (btw the row and column are for something else, not the button - in a different function). This is my code, I can't seem to understand how to get the most updated value for a button since all of my buttons are getting the values of the first button except for the text name and their column

for order in self.orders:
            if counter2 >= 2 and counter2 % 2 == 0:
                x += 1
            print x, counter2
            bt = Button(window, command=lambda i=counter2: self.display_order(self.orders[order], window, x, i))
            bt.configure(text='order ' + str(counter2+1), fg='black', bg='steel blue', width=20)
            bt.grid(sticky='W', row=0, column=counter2, columnspan=1)  # increase row number for every button
            counter2 += 1
Lior shem
  • 31
  • 6
  • Don't know what does it have to do with the thread "Creating functions in a loop" because mine is about tkinter and the update of lambda function. – Lior shem Mar 29 '18 at 19:12
  • You accepted that answer but you don't understand how the question I linked is relevant? I'm confused. – Aran-Fey Mar 29 '18 at 19:14
  • I guess I did not completely understand the other thread, I know that in tkinter the lambdas are used to get the most updated value but didn't know you could use more than one. – Lior shem Mar 29 '18 at 19:18

1 Answers1

0

x has also a problem with late binding. You've done it right for counter2. Use the following change (by adding x=x as additional lambda argument):

bt = Button(window, command=lambda i=counter2, x=x: self.display_order(self.orders[order], window, x, i))
Günther Jena
  • 3,706
  • 3
  • 34
  • 49