How can I pass arguments to Tkinter button's callback command?

后端 未结 2 1944
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 11:15

I got 2 buttons, respectively named \'ButtonA\', \'ButtonB\'. I want the program to print \'hello, ButtonA\' and \'hello, ButtonB\' if any button is clicked. My code is as f

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 11:32

    This should work:

    ...
    btnaText='ButtonA'
    btna = Button(root, text = btnaText, command = lambda: sayHi(btnaText))
    btna.pack()
    

    For more information take a look at Tkinter Callbacks

提交回复
热议问题