如何通过单击PyQt中的按钮将参数传递给函数? [英] How to pass arguments to functions by the click of button in PyQt?
本文介绍了如何通过单击PyQt中的按钮将参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在单击按钮时将参数传递给函数.我应该在此行button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))
中添加什么,以便它将值传递给函数:
I want to pass the arguments to a function when I click the button. What should I add to this line button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))
so it will pass the value to the function:
def calluser(name):
print name
def Qbutton():
button = QtGui.QPushButton("button",widget)
name = "user"
button.setGeometry(100,100, 60, 35)
button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))
另一件事,将使用for
循环生成按钮;因此name
值会有所不同.因此,我想在按钮上附加每个名称.通过使用for
循环并在单击时调用基函数自变量,我在Pytk中做了同样的事情.
One more thing, buttons will be generated using for
loop; so name
value will vary. So I want to attach each name with the button. I have done same thing in Pytk by using for
loop and calling the argument base function when clicked.
推荐答案
您只需编写
name = "user"
button.clicked.connect(lambda: calluser(name))
这篇关于如何通过单击PyQt中的按钮将参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文