PyQt:如何连接 QComboBox 以使用参数运行 [英] PyQt: How to connect QComboBox to function with Arguments
本文介绍了PyQt:如何连接 QComboBox 以使用参数运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
QComboBox 使用以下语法连接到函数:
QComboBox is connected to a function using following syntax:
myComboBox.activated.connect(self.myFunction )
但我需要能够将参数从 ComboBox 发送到 myFunction().但如果我使用:
But I need to be able to send the arguments from ComboBox to myFunction(). But if I use:
myComboBox.activated.connect(self.myFunction(myArg1, myArg2 )
我得到了
TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'
需要使用什么语法将 QComboBox 连接到能够接收从 Comobobox 发送的参数的函数?
What syntax needs to be used to connect a QComboBox to a function that is able to receive arguments sent from Comobobox?
这是导致 TypeError 的代码:
Here is the code resulting an TypeError:
connect() 槽参数应该是可调用的或信号,而不是 'NoneType'
from PyQt4 import QtCore, QtGui
import sys
class MyClass(object):
def __init__(self, arg):
super(MyClass, self).__init__()
self.arg = arg
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItems([str(x) for x in range(3)])
self.myObject=MyClass(id(self) )
self.comboBox.activated.connect(self.myFunction(self.myObject, 'someArg'))
def myFunction(self, arg1=None, arg2=None):
print '\n\t myFunction(): ', type(arg1),type(arg2)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myApp')
dialog = myWindow()
dialog.show()
sys.exit(app.exec_())
推荐答案
在我发布问题后,Stachoverflow 建议了一个链接,其中解释了很多.答案如下:
After I posted a question Stachoverflow suggested a link which explained a lot. Here is the answer:
from PyQt4 import QtCore, QtGui
class MyClass(object):
def __init__(self, arg):
super(MyClass, self).__init__()
self.arg = arg
class myWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItems([str(x) for x in range(3)])
self.myObject=MyClass(id(self) )
slotLambda = lambda: self.indexChanged_lambda(self.myObject)
self.comboBox.currentIndexChanged.connect(slotLambda)
@QtCore.pyqtSlot(str)
def indexChanged_lambda(self, string):
print 'lambda:', type(string), string
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myApp')
dialog = myWindow()
dialog.show()
sys.exit(app.exec_())
这篇关于PyQt:如何连接 QComboBox 以使用参数运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文