0

I created a calculator using PyQt5 designer, in the logic part written in Python each button call a class to add the corresponding symbol like this:

 self.button_0.clicked.connect(self.push0)
 self.button_1.clicked.connect(self.push1)
 def push0(self):
        t=self.display.toPlainText()
        self.display.setText(t+"0")
    def push1(self):
        t=self.display.toPlainText()
        self.display.setText(t+"1")

But if i try to write one function for all of them like:

    self.button_0.clicked.connect(self.push(self, '0'))
    self.button_0.clicked.connect(self.push(self, '1'))
    def push(self, c):
        t=self.display.toPlainText()
        self.display.setText(t+c)

I get the message "TypeError: push() takes 2 positional arguments but 3 were given" What's the problem? Thans to everyone already

  • Change to `self.button_0.clicked.connect(lambda: self.push('0'))` – musicamante Aug 27 '21 at 10:13
  • What's the difference between: self.button_0.clicked.connect(lambda: self.push('0')) and self.button_0.clicked.connect(self.push(self, '0')) – Jacopo Bonanno Aug 27 '21 at 10:18
  • It's explained in the accepted answer for which your question is marked as duplicate: the connection argument needs to be a *callable*, but you're actually *running* `push`, so the argument is not the callable, but its result (which seems to be `None` in your case). Using lambda gives a reference to a *callable function*, without executing it. – musicamante Aug 27 '21 at 10:45

0 Answers0