AttributeError: 'bool' object has no attribute 'le' — pyqt — getting text into python variable

后端 未结 1 1459
误落风尘
误落风尘 2020-12-12 05:50

I have created a UI using Qt Designer which has a simple line input and a push button. I tried getting the input of line edit in a python variable but it is throwing me an e

相关标签:
1条回答
  • 2020-12-12 06:49

    You are misusing the "self" argument, as the function itself should be in the class indentation (thus, it should be a class's method) or receive the instance as argument.

    In your code, functioni will only receive the signal argument (all buttons, including QPushButtons, have a checked argument). This means that you are calling functioni with False as argument (QPushButton's checked state after clicking), and self will actually be a bool variable. Remember that self is just a convention in python, it actually is a normal positional argument, you could call it as you want.

    To solve your problem, it's enough to make the functioni function a class method by adding the right indentation (and calling self.functioni in the signal connection). If, for any reason, you need to leave the function outside the class, you could either add the text or the class instance as an argument:

        self.pushButton.clicked.connect(lambda checked: functioni(self.le.text())
        self.pushButton.clicked.connect(lambda checked: functioni(self))
    



    Unrelated, but important. Remember that the second execute argument for sqlite has to be an iterable (tuple, list, etc.). If you only have one parameter, a single value tuple can be obtained by adding a comma before closing the parenthesis:

    cur.execute("INSERT INTO Name (Name) VALUES (?)", (self.le.text(), ))
    
    0 讨论(0)
提交回复
热议问题