使用以下语法将QComboBox连接到函数:
myComboBox.activated.connect(self.myFunction )
但是我需要能够将ComboBox中的参数发送到myFunction()函数。但是如果我使用:
myComboBox.activated.connect(self.myFunction(myArg1, myArg2 )
我正在获得
TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'
如何连接QComboBox到一个能够接收从Comobobox发来的参数的函数?
稍后编辑:
这是导致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_())
self.comboBox.activated.connect(self.myFunction)
,就可以了。不需要在连接时指定参数。 - Raydel MirandaQComboBox
的信号activate
是使用int
发出的,请参见:http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html#activated。因此,你必须定义自己的信号,一个发出你想要的值类型的信号(对于self.myObject
是MyClass
,对于someArg
是str
)。然后,你可以从处理activate
事件的函数中发出你的信号。当然,你自己的信号也需要一个处理程序/槽。 - Raydel MirandamyFunction
时,您调用了它,因此将None
传递给connect
而不是myFunction
本身。解决方案是使用临时函数包装调用并传递它:lambda: self.myFunction(self.myObject, 'someArg')
。这是解决此问题的标准方法。例如,在tkinter
中,您也可以以同样的方式解决它。 - z33k