如何通过单击 PyQt 中的按钮将参数传递给函数? [英] How to pass arguments to functions by the click of button in PyQt?

查看:62
本文介绍了如何通过单击 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 值会有所不同.所以我想用按钮附加每个名称.我在 Pytk 中做了同样的事情,使用 for 循环并在点击时调用参数基函数.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆