-2

I would like the user to input a value in lineEdit_0 and print the value in the text browser. But I found the below error. Might I know how to solve it?

printf() takes 2 positional arguments but 3 were given

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        #MainWindow Title
        self.setWindowTitle("Pandora Box")

        #Set Window Icon
        self.setWindowIcon(QtGui.QIcon(r''))

        # Background Palette
        palette = QtGui.QPalette()
        palette.setBrush(self.backgroundRole(), QtGui.QColor(70,70,70))
        self.setPalette(palette)

        #StatusBar
        self.statusBar().showMessage("Coming Soon")

            # Text Browser
            self.tb = self.ui.textBrowser
            self.tb.setAcceptRichText(True)
            self.tb.setOpenExternalLinks(True)
    
            #Click Button Effect
            self.ui.pushButton.setText('Display')
            text = self.ui.lineEdit_0.text() #Error 
    
            self.ui.pushButton.clicked.connect(self.printf(self, text)) #Error
    
    
        def printf(self, mes):
            self.tb.append(mes)  
            self.cursot = self.tb.textCursor()
            self.tb.moveCursor(self.cursot.End)
    
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication([])
        mainWindow = MainWindow()
        mainWindow.show()
        sys.exit(app.exec())
Asker
  • 1,299
  • 2
  • 14
  • 31
AnsonChan
  • 43
  • 8
  • there is no need to give **self** as agrument here: ```self.ui.pushButton.clicked.connect(self.printf(self, text)) #Error``` try to use ```self.ui.pushButton.clicked.connect(self.printf(text))``` – crackanddie Dec 28 '20 at 05:05
  • It pops up another error "argument 1 has unexpected type 'NoneType'" – AnsonChan Dec 28 '20 at 05:26
  • ok, can you give me Ui_MainWindow class so then I can recreate the code and run it. And check please that **text** variable is not None type – crackanddie Dec 28 '20 at 05:30
  • @crackanddie the ui doesn't matter, the reason is simple: a signal connection expects a *reference to a callable*. – musicamante Dec 28 '20 at 07:54

1 Answers1

1

When a signal connection is created, the argument of connect() must be a reference to a callable.

This means that if the function should be executed as self.printf(), the connection must be done without the parentheses:

        self.ui.pushButton.clicked.connect(self.printf)

Note that your attempt to add the text argument in the connection is wrong not only for the reason above, but also because you will probably want to use the text of the line edit as it is when the function is called, but text = self.ui.lineEdit_0.text() will only give you the text at the moment in which you are trying to connect (which will probably be an empty string). Also, you added a self argument to the function, which is clearly wrong.

The problem is that your printf function expects an argument that is not compatible with the clicked signal argument (which is a bool), and you also want to use a dynamic value for that argument, based on the line edit contents.

So, you either avoid the argument at all:

        self.ui.pushButton.clicked.connect(self.printf)
        # ...

    def printf(self):
        mes = self.ui.lineEdit_0.text()
        self.tb.append(mes)
        # ...

Or you use a lambda in the connection:

        self.ui.pushButton.clicked.connect(lambda: self.printf(self.ui.lineEdit_0.text()))
musicamante
  • 41,230
  • 6
  • 33
  • 58