本文介绍了如何将参数传递给 Tkinter 按钮的回调命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个按钮,分别命名为ButtonA"、ButtonB".如果单击任何按钮,我希望程序打印hello, ButtonA"和hello, ButtonB".我的代码如下:

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()

当我点击ButtonA时,发生错误,text not defined.

When I click ButtonA, error occurs, text not defined.

我理解这个错误,但我如何将 ButtonA 的文本传递给 lambda?

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()

有关更多信息,请查看 Tkinter 回调

For more information take a look at Tkinter Callbacks

这篇关于如何将参数传递给 Tkinter 按钮的回调命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:07