Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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 error. This is my python code:

from PyQt4 import QtGui
import sys
import prog
import MySQLdb

class ExampleApp(QtGui.QMainWindow, prog.Ui_Program):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(functioni)

def functioni (self):
    db = MySQLdb.connect(host="localhost", 
                     user="root",       
                     passwd="*****",    
                     db="testpy")   

    cur = db.cursor()

    cur.execute("INSERT INTO Name (Name) VALUES (?)", self.le.text()) #self.le.text() is giving me trouble...

    db.commit()
    cur.close()
    db.close()


def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

My ui code has this:

self.le = QtGui.QLineEdit(self.centralwidget)
self.le.setObjectName(_fromUtf8("le"))

When I run the python program, I get this error:

Traceback (most recent call last):
  File "main.py", line 20, in functioni
  cur.execute("INSERT INTO Name (Name) VALUES (?)", self.le.displayText())
AttributeError: 'bool' object has no attribute 'le'

This question might have been asked before but I've tried everything and it won't work! I can't figure what is bool here. The solution might be trivial but I'd appreciate it if someone could point my error. Thank you.

Here is my full ui code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'prog.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Program(object):
    def setupUi(self, Program):
        Program.setObjectName(_fromUtf8("Program"))
        Program.resize(351, 138)
        Program.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.centralwidget = QtGui.QWidget(Program)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.formLayout = QtGui.QFormLayout(self.centralwidget)
        self.formLayout.setObjectName(_fromUtf8("formLayout"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setObjectName(_fromUtf8("label"))
        self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.label)
        self.le = QtGui.QLineEdit(self.centralwidget)
        self.le.setObjectName(_fromUtf8("le"))
        self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.le)
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.formLayout.setWidget(1, QtGui.QFormLayout.FieldRole, self.pushButton)
        Program.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(Program)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 351, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuSubmit = QtGui.QMenu(self.menubar)
        self.menuSubmit.setObjectName(_fromUtf8("menuSubmit"))
        Program.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(Program)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        Program.setStatusBar(self.statusbar)
        self.toolBar = QtGui.QToolBar(Program)
        self.toolBar.setObjectName(_fromUtf8("toolBar"))
        Program.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionSubmit = QtGui.QAction(Program)
        self.actionSubmit.setObjectName(_fromUtf8("actionSubmit"))
        self.menuSubmit.addAction(self.actionSubmit)
        self.menubar.addAction(self.menuSubmit.menuAction())

        self.retranslateUi(Program)
        QtCore.QMetaObject.connectSlotsByName(Program)

    def retranslateUi(self, Program):
        Program.setWindowTitle(_translate("Program", "Program", None))
        self.label.setText(_translate("Program", "Name", None))
        self.pushButton.setText(_translate("Program", "Submit", None))
        self.menuSubmit.setTitle(_translate("Program", "Submit", None))
        self.toolBar.setWindowTitle(_translate("Program", "toolBar", None))
        self.actionSubmit.setText(_translate("Program", "Submit", None))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(), ))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...