如何在 PyQT 中使用预定义的 SIGNAL 将参数传递给函数 [英] how to pass arguments to a function using a predefined SIGNAL in PyQT

查看:51
本文介绍了如何在 PyQT 中使用预定义的 SIGNAL 将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在一个类中有 3 个 QRadioButtons 实例

lets suppose you have 3 QRadioButtons instances inside a class

self.Option1 = QRadioButton()
self.Option2 = QRadioButton()
self.Option2 = QRadioButton()

(为简洁起见,我没有编写整个脚本)并且您希望在用户单击时使用它们来执行特定功能,因此您可以这样做

(for brevity i didn't wrote the entire script) and you want to use them to execute a particular function when the user clicks it, so you do

self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction)

我如何将参数传递给 myFunction 以便它知道用户单击了哪些 QRadioButton?所以我可以做像

How do i pass arguments to to myFunction so it knows wich of the QRadioButtons was clicked by the user? so i can do stuff like

def myFunction(choice):
   if choice == Option1:
       do something
   if choice == Option2:
       do something

现在我有 3 个函数,每个函数都用于像这样的每个连接

Right now i have 3 functions, each for every connect like this

self.connect(self.Option1,SIGNAL("clicked()"), self.myFunction1)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)
self.connect(self.Option2,SIGNAL("clicked()"), self.myFunction2)

而且这种方法工作得很好,但是您可以看到代码的增长速度有多快,因为对于我添加的每个小部件,我都必须至少编写一个新函数或修改现有函数,这将是维护的噩梦(更不用说它看起来很丑,而且会阻止代码重用).

And this approach works just fine, but you can see how quickly the code can grow because with every widget i add i'll have to write at least one new function or modify the existing ones, its going to be a maintainance nightmare(not to mention that it looks ugly and it prevents code reuse).

对谷歌的快速访问让我得到了这个:

A quick visit to google got me this:

    self.label = QLabel(" ")

    self.connect(self, SIGNAL("didSomething"),
                 self.update_label)
    self.do_something()

def do_something(self):
    self.emit(SIGNAL("didSomething"), "important", "information")

def update_label(self, value1, value2):
    self.label.setText(value1 + " " + value2)

(同样我没有包含整个脚本,但你可以查看它这里)他所做的是创建一个方法来定义一个自定义发射器,该发射器在发射时发送参数,然后使用

(again i didnt include the entire script but you can check it out here) What he did was creating a method to define a custom emitter that sends arguments when emitted and then activated it manually using

self.do_something()

很自然地,connect 函数选择了这个发射并将参数传递给函数 update_label,部分实现了我想要做的.但他是手动完成的,有没有办法自动拦截"标准信号(例如 clicked() 信号 QRadioButtons 发出)并向其添加参数,以便将它们传递给函数以使用它们?

So naturally, the connect function picks this emition and pass the arguments to the function update_label, partially achieving what i want to do. But he is doing it manually, is there a way to automatically "intercept" standard signals(for example the clicked() signal QRadioButtons emit) and add arguments to it, so they get passed to a function to work with them?

提前致谢

推荐答案

Qt 这个问题的解决方案我觉得是 QSignalMapper.但是我之前遇到过同样的问题,我发现使用部分"功能更简单.像这样使用它:

The Qt solution of this problem is QSignalMapper I think. But I had the same problem before, and I figured out using "partial" function is simplier. Use it like that:

from functools import partial

self.connect(self.Option1,SIGNAL("clicked()"), partial(self.myFunction, 1))
self.connect(self.Option2,SIGNAL("clicked()"), partial(self.myFunction, 2))
self.connect(self.Option3,SIGNAL("clicked()"), partial(self.myFunction, 3))

更多信息:http://docs.python.org/library/functools.html#functools.partial

这篇关于如何在 PyQT 中使用预定义的 SIGNAL 将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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