0

In this snippet of code I am attempting to pass a parameter to buttonClicked() and it is failing with parameter of type NoneType. Is there a reason why my function is not accepting the "1" string parameter?

import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication 

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        btn1 = QPushButton("Button 1", self)
        btn1.move(30, 50)

        # This code ties the slots and signals together
        # clicked is the SIGNAL
        btn1.clicked.connect(self.buttonClicked("1"))

        self.statusBar()
        self.setGeometry(300, 300, 300, 230)
        self.setWindowTitle('Event sender')
        self.show()


    def buttonClicked(self, buttonNumber):
        sender = self.sender()
        self.statusBar().showMessage(buttonNumber + ' was pressed')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Not a machine
  • 508
  • 1
  • 5
  • 21
  • 1
    connect expects a callable as a function or method, but you are passing a function already evaluated that is no longer a callable but a value, a way to add arguments to callable is using functools.partial – eyllanesc Apr 18 '19 at 20:37
  • 1
    1) add `from functools import partial` 2) change to `btn1.clicked.connect(partial(self.buttonClicked, "1"))` – eyllanesc Apr 18 '19 at 20:43
  • Thank you. I searched and did not see the answer to which you linked. – Not a machine Apr 18 '19 at 21:54
  • I have been using Python for quite a few years and this is the first time I have encountered this situation. Thank you for the clear explanation. – Not a machine Apr 18 '19 at 22:03

0 Answers0