本文介绍了如何传递参数给Tkinter的按钮的回调命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个按钮,分别命名为按钮a','ButtonB。
我想如果单击任何按钮,程序打印你好,按钮a'和'你好,ButtonB。
我的code是如下:
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 follows:
def sayHi(name):
print 'hello,', name
root = Tk()
btna = Button(root, text = 'ButtonA', command = lambda: text)
btna.pack()
当我点击按钮a,发生错误时,文本没有定义
。
When I click ButtonA, error occurs, text not defined
.
我明白了这个错误,但我怎么能通过按钮a的文本拉姆达?
I understand this error, but how can I pass ButtonA's text to lambda?
推荐答案
这应该工作:
...
btnaText='ButtonA'
btna = Button(root, text = btnaText, command = lambda: sayHi(btnaText))
btna.pack()
有关详细信息,看一看
For more information take a look at Tkinter Callbacks
这篇关于如何传递参数给Tkinter的按钮的回调命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!