如何通过单击 PyQt 中的按钮将参数传递给函数?
我想在单击按钮时将参数传递给函数。我应该在这一行添加什么 button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))
以便它将值传递给函数:
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 循环并在单击时调用参数基函数。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以简单地写
You can simply write
通常 GUI 是使用类构建的。通过使用绑定方法作为回调(请参阅下面的
self.calluser
),您可以通过self
的属性(例如self.name< /code>):
例如,使用 本教程 中稍微修改过的代码
:当以下情况时,se 总是被调用而无需附加参数您需要将不同的附加信息传递给许多回调,您需要为每个按钮进行不同的回调。
由于这可能很费力(如果手动完成),请改用函数工厂。请参阅下面的示例。函数工厂是一个闭包。它可以传递额外的参数,内部函数可以在调用时访问这些参数:
Usually GUIs are built using classes. By using bound methods as callbacks (see
self.calluser
below) you can "pass" information to the callback throughself
's attributes (e.g.self.name
):For example, using slightly modified code from this tutorial:
Since the callback per se is always called with no additional arguments, when you need to pass distinct additional information to many callbacks, you need to make different callbacks for each button.
Since that can be laborious (if done manually), use a function factory instead. See below for an example. The function factory is a closure. It can be passed additional arguments, which the inner function can access when called:
我尝试了一种有效的方法,结果对我来说效果很好。您可以使用代码:
I tried an efficient way of doing it and it worked out well for me. You can use the code:
在Python中,类实例是可调用的。您可以只使用类的实例作为函数。该类可以包含您想要的任何内容。 (在某些语言或框架中,可调用对象称为函子或函数对象。)
In Python, class instances are callable. You can just use an instance of a class as a function. That class can contain whatever you want. (In some languages or frameworks, a callable object is called a functor or a function object.)
这是另一种方法。 --- 部分 -- 我发现这个最简单:
可以在以下位置找到:
http://tech-artists.org/forum/showthread.php?3118-PyQt-Maya-How-to-pass-arguments-to-a-function-when-connecting-it-to- PyQt-按钮
Here is another way. --- PARTIAL -- I find this one most simple:
found on:
http://tech-artists.org/forum/showthread.php?3118-PyQt-Maya-How-to-pass-arguments-to-a-function-when-connecting-it-to-PyQt-button
下面显示的代码说明了将数据与生成的按钮相关联的方法。例如,您可以更改语句
self.keydata[b] = key
将数据元组存储到self.keydata[b]
中,以便在处理按钮事件时使用之后。请注意,在以下代码中,
assets
是先前定义的包含按钮标题的字典。在processButton(self)
例程中,self.sender()
等于类变量buttons[]
中的条目。输出如下所示,其中前四行在 for 循环期间打印,最后四行在按顺序按下四个按钮时打印。
The code shown below illustrates a method of associating data with generated buttons. For example, you could change the statement
self.keydata[b] = key
to store a tuple of data intoself.keydata[b]
for use when processing a button event later.Note, in the following code,
assets
is a previously-defined dictionary containing titles for buttons. In theprocessButton(self)
routine,self.sender()
is equal to an entry in the class variablebuttons[]
.Output was like the following, where the first four lines printed during the
for
loop, and the last four printed when the four buttons were pressed in order.您也可以使用参数,
函数
You can do with parameters as well,
the function