I'm trying to create a loop that creates several buttons and displays them on the screen but I'm running into problems when trying to identify each specific button.
I've tried several things, but I never seem to be able to send the correct 'id' of the button to my function.
This is the last thing I've tried:
class Settings(ttk.Frame):
def toggle(self, button):
print(button)
def __init__(self, root):
super().__init__(root)
self.dictionary = {}
self.buttons = []
count = 0
row_count = 0
column_count = 0
for coin in calculate.denomination:
self.dictionary[f"bt_{count}"] = coin
if coin[2]:
self.buttons.append(ttk.Button(self, text=f"Disable {coin[0]}",
command=lambda: self.toggle(self.dictionary[f'bt_{count}'])))
else:
self.buttons.append(ttk.Button(self, text=f"Enable {coin[0]}"))
self.buttons[count].grid(row=row_count, column=column_count, padx=5, pady=5, sticky=tk.W)
count += 1
row_count += 1
if row_count == 6:
row_count = 0
column_count += 1
The command attribute for every button always ends up with the last value of the count variable ('bt_14'). I'm guessing that the reason for this is that it's passing the variable itself and not its value. I've tried to circumvent this using .copy() and also coping the value to another variable but it didn't work.