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