且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PyQT 中使用预定义的 SIGNAL 将参数传递给函数

更新时间:2023-02-26 16:55:24

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