0

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
whygod
  • 1
  • 1
    Welcome to Stack Overflow. You do, in fact, pass the value rather than the variable itself, and you *cannot possibly* pass the variable. This is a simple consequence of the fact that there *isn't necessarily* a value to pass. The problem occurs for a different reason, which is a very common problem in Python especially when using tkinter. I have linked a duplicate for you. – Karl Knechtel Nov 30 '21 at 00:05
  • 1
    Incidentally, though: if you want to use consecutive integers in order to identify your coin values, it would be a lot simpler to just put them in a list that you index into directly, rather than making up string keys for a dict. If you want to *associate* the coin value with the corresponding button directly, then you could use a dict - where the buttons are the keys. But I don't think you really need to do that, because *all the necessary information from `coin` gets baked into the button when you create it*. – Karl Knechtel Nov 30 '21 at 00:07
  • 1
    Also, as a heads up for future questions: please read the [formatting help](https://stackoverflow.com/help/formatting) and make sure you understand how to post your code so that the indentation is preserved. If you use leading space to mark a code block, you must put that space in front of *each* line (and ensure that there is a blank line before and after your code.) – Karl Knechtel Nov 30 '21 at 00:10

0 Answers0