Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dynamic button in PyQt

Tags:

python

qt

pyqt

I try to add a function in a PyQt class, but it always returns me an error.

# 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

May be someone here can help me to solve that :') Thx !

like image 825
MObject Avatar asked May 24 '12 01:05

MObject


2 Answers

You need a function:

button.clicked.connect(lambda: self.commander(command))

Note the lambda will avoid the evaluation of the function call, so it'll call self.commander(command) only when clicked

like image 121
JBernardo Avatar answered Oct 19 '22 05:10

JBernardo


appears that in

button.clicked.connect(self.commander(command))

self.commander(command) is returning None instead of a signal or a callable.

like image 27
Skylar Saveland Avatar answered Oct 19 '22 05:10

Skylar Saveland