Creating a dynamic button in PyQt

I am trying to add a function to the PyQt class, but always returns an error to me.

# 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 

Maybe someone here can help me solve this: ') thanks!

+6
source share
2 answers

You need a function:

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

Note that lambda will avoid evaluating a function call, so it will only call self.commander(command) when pressed

+21
source

appears that in

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

self.commander(command) returns None instead of the signal or called.

+2
source

Source: https://habr.com/ru/post/916514/


All Articles