我试图在一个PyQt类中添加一个函数,但它总是返回错误。
# Error: TypeError: connect() slot argument should be a callable or a signal, not 'NoneType' #
def commander (self, arg):
exec arg
def aButton (self, layout, **kwargs):
name = kwargs.pop("name","Button")
command = kwargs.pop("command", "" )
button = QtGui.QPushButton(name)
button.clicked.connect(self.commander(command))
layout.addWidget(button)
return button
也许这里有人可以帮我解决这个问题:') 谢谢!
lambda
,那个代码块self.commnader(command)
将在表达式中被评估,之后不会再次执行。结果(无论是什么)将传递给connect
方法。使用lambda
,你将告诉解释器这是一段稍后要执行的代码(因此它将成为只有一个语句的函数)。然后将该函数传递给connect
方法。 - JBernardo