Dec-06-2020, 07:43 PM
OH! loking at your code I can now see what I was doing wrong. I misunderstood the lambda command but I think I understand now.
Thank you very much!
Thank you very much!
(Dec-06-2020, 07:27 PM)deanhystad Wrote: I don't know how much benefit there is in making buttons in a loop when they are bound to different functions. Unless the functions are all the same function with different arguments.
from tkinter import * def grid_fill(frame): colors = ['red', 'green'] k = 0 for j in range(5): for i in range(5): k += 1 color = colors[i % len(colors)] b = Button(frame, text = str(k), width = 3, height = 2, bg=color) b.configure(command=lambda x=k: print(x)) b.grid(row=j, column=i) grid_fill(Tk()) mainloop()I'm doing three things here
1. Calculating the button label.
2. Looking up the button color.
3. Setting a callback with button specific arguments.