Python PyQt:如何连接 QComboBox 以使用参数运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23116763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyQt: How to connect QComboBox to function with Arguments
提问by alphanumeric
QComboBox is connected to a function using following syntax:
QComboBox 使用以下语法连接到函数:
myComboBox.activated.connect(self.myFunction )
But I need to be able to send the arguments from ComboBox to myFunction(). But if I use:
但我需要能够将参数从 ComboBox 发送到 myFunction()。但如果我使用:
myComboBox.activated.connect(self.myFunction(myArg1, myArg2 )
I am getting
我正进入(状态
TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'
What syntax needs to be used to connect a QComboBox to a function that is able to receive arguments sent from Comobobox?
需要使用什么语法将 QComboBox 连接到能够接收从 Comobobox 发送的参数的函数?
EDITED LATER:
稍后编辑:
Here is the code resulting an TypeError:
这是导致 TypeError 的代码:
connect() slot argument should be a callable or a signal, not 'NoneType'
connect() slot argument should be a callable or a signal, not '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_())
采纳答案by alphanumeric
After I posted a question Stachoverflow suggested a link which explained a lot. Here is the answer:
在我发布了一个问题之后,Stachoverflow 建议了一个链接,该链接解释了很多。答案如下:
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_())