问题
Using QT Designer to design the code. Then using the pyuic5 -x productentryscreen.ui > productentryscreen3.py
command to generate python code.
i am using buttons and textboxes. when a button is pressed i want to display value entered in the QlineEdit box.
But i am getting the error " AttributeError: 'bool' object has no attribute" when the btnCancel is clicked. This is where the error is thrown (i presume) textboxValue = self.txtCustCode.text
. i even tried with textboxValue = self.txtCustCode.text()
Following is the code
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit,
QMessageBox
# Generated Code
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#def setupUi(self):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 130, 160, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 160, 130, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 190, 120, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 220, 180, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(30, 240, 170, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(80, 30, 481, 31))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.txtHMI = QtWidgets.QLineEdit(self.centralwidget)
self.txtHMI.setGeometry(QtCore.QRect(230, 130, 211, 20))
self.txtHMI.setObjectName("txtHMI")
self.txtCustCode = QLineEdit(self.centralwidget)
self.txtCustCode.setGeometry(QtCore.QRect(230, 160, 211, 20))
self.txtCustCode.setObjectName("txtCustCode")
self.txtProdCode = QtWidgets.QLineEdit(self.centralwidget)
self.txtProdCode.setGeometry(QtCore.QRect(230, 190, 211, 20))
self.txtProdCode.setObjectName("txtProdCode")
self.txtCustPartNo = QtWidgets.QLineEdit(self.centralwidget)
self.txtCustPartNo.setGeometry(QtCore.QRect(230, 220, 211, 20))
self.txtCustPartNo.setObjectName("txtCustPartNo")
self.txtLampName = QtWidgets.QLineEdit(self.centralwidget)
self.txtLampName.setGeometry(QtCore.QRect(230, 250, 211, 20))
self.txtLampName.setObjectName("txtLampName")
self.btnSave = QtWidgets.QPushButton(self.centralwidget)
self.btnSave.setGeometry(QtCore.QRect(130, 280, 131, 41))
self.btnSave.setObjectName("btnSave")
self.btnSave.clicked.connect(btnSave_click)
self.btnCancel = QtWidgets.QPushButton(self.centralwidget)
self.btnCancel.setGeometry(QtCore.QRect(330, 280, 131, 41))
self.btnCancel.setObjectName("btnCancel")
self.btnCancel.clicked.connect(btnCancel_click)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Product as in HMI"))
self.label_2.setText(_translate("MainWindow", "Customer Code"))
self.label_3.setText(_translate("MainWindow", "Product Code"))
self.label_4.setText(_translate("MainWindow", "Customer Part Number"))
self.label_5.setText(_translate("MainWindow", "Lamp Name & Type"))
self.label_6.setText(_translate("MainWindow", "Products Data Entry / Update Screen"))
self.btnSave.setText(_translate("MainWindow", "SAVE"))
self.btnCancel.setText(_translate("MainWindow", "Cancel"))
# End Generated Code
@pyqtSlot() # Signals buttons
def btnCancel_click(self):
textboxValue = "Dummy"
textboxValue = self.txtCustCode.text
#QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok)
print (textboxValue)
cancelFunction()
def btnSave_click(self):
print ("Save Clicked")
insertFunction()
# Functions for signals
def insertFunction():
print ("Save Clicked")
def cancelFunction():
print ("Cancel Clicked")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
回答1:
Below line returns a boolean value.
self.txtCustCode = QLineEdit(self.centralwidget)
Then you are trying to using setGeometry
attribute. That's why it gives you error.
self.txtCustCode.setGeometry(QtCore.QRect(230, 160, 211, 20))
self.txtCustCode.setObjectName("txtCustCode")
来源:https://stackoverflow.com/questions/59223384/pyqt5-attributeerror-bool-object-has-no-attribute-txtcustcode