0

I am developing an app with tkinter and I have the following code:

tricks = ['Always Three', 'bhu']

        def trickInstructions(selectedTrick):
            print(selectedTrick)

        def menu():
            for trick in tricks:
                Button(root, text = trick, pady=1, command = lambda: trickInstructions(self.name)).pack(side=BOTTOM) 

I would like to send the name of the button to the trickInstructions() function everytime the button is clicked.

Is this possible and how?

Alex Hawking
  • 1,125
  • 5
  • 19
  • 35
  • You might be looking for [a solution like this](https://stackoverflow.com/a/50762215/). – metatoaster Jul 27 '20 at 11:42
  • @metatoaster I don't really understand this answer and if it applies... – Alex Hawking Jul 27 '20 at 11:43
  • 1
    Change `command = lambda: trickInstructions(self.name)` to `command=lambda name=trick: trickInstructions(name)`. – acw1668 Jul 27 '20 at 11:44
  • If you tried the answer I linked the answer is very obvious, because it literally prints out the text of the button being sent via the callback. – metatoaster Jul 27 '20 at 11:46
  • @acw1668 this worked perfectly, if you write this as an answer I will mark it as correct – Alex Hawking Jul 27 '20 at 11:48
  • Also please read the question and related answers within the thread to get the full background, rather than just glancing at a given answer for less than a minute and say you don't understand - it hints to me that you might not appreciate an actual answer. – metatoaster Jul 27 '20 at 11:49
  • @metatoaster I have limited knowledge of python and tkinter but this seems like a longer than neccessary way of accomplishing what I want. This has been proven by acw1668s single line answer, I appreciate your input but it was not what I was looking for – Alex Hawking Jul 27 '20 at 11:52

1 Answers1

1

You can pass the text using lambda via default value of argument:

command=lambda name=trick: trickInstructions(name)
acw1668
  • 40,144
  • 5
  • 22
  • 34