0

I'm trying to bind a function to all the buttons in my App and pass Extra Parameters as well

However, all the buttons getting the same parameters instead of a unique parameter for each one(the parameters of the last button in the for loop)

Any suggestions on what am I doing wrong?

def add_buttons(self, letters):
    for i, row in enumerate(letters):
        for j, letter in enumerate(row):
            btn = Button(self._bottom_mid, text=letter, font=self._myFont, bg=WHITE_COLOR,
                     fg=BLUE_COLOR, padx=30, pady=30)
            btn.place(relx=i * 0.2 + 0.1, rely=j * 0.2 + 0.1, relwidth=0.2, relheight=0.2)
            btn.bind("<Button-1>", lambda e: self._boggle.moused_clicked(e, (i, j), letter))


def moused_clicked(e, coordinate, letter):
    print(coordinate)
    print(letter) 
    # The output is always the coordinate of
    # the last button no matter what button was clicked on
  • Also you shouldn't bind to the button but instead use its `command` argument. – TheLizzard Jun 15 '21 at 18:37
  • why so many people recently are trying to bind something to a button? especially Button-1? why is that? because Button has a `command` argument used for exactly that. just place the command in there (unless for some reason You need the passed event which I can't imagine being used anyways), also for this (unlike when using command argument where it is way simpler) You will have to store those buttons in a list and reference them that way and then bind to them – Matiiss Jun 15 '21 at 18:37
  • Thanks for your answers! I tried the command as well however it still got the same results. – Naveh Mevorach Jun 15 '21 at 18:41

0 Answers0