本文介绍了pyQt5 AttributeError:'布尔'对象没有属性'txtCustCode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用QT Designer设计代码.然后使用pyuic5 -x productentryscreen.ui > productentryscreen3.py命令生成python代码.我正在使用按钮和文本框.当按下按钮时,我要显示在QlineEdit框中输入的值.但是单击btnCancel时出现错误"AttributeError:'bool'对象没有属性".这是引发错误(我认为)textboxValue = self.txtCustCode.text的地方.我什至尝试了textboxValue = self.txtCustCode.text()以下是代码

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

推荐答案

下面的行返回布尔值.

self.txtCustCode = QLineEdit(self.centralwidget)

然后,您尝试使用setGeometry属性.这就是为什么它会给您错误.

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")

这篇关于pyQt5 AttributeError:'布尔'对象没有属性'txtCustCode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:18