使用QT Designer设计代码。然后使用pyuic5 -x productentryscreen.ui > productentryscreen3.py
命令生成python代码。
我正在使用按钮和文本框。当按下按钮时,我要显示在QlineEdit框中输入的值。
但是单击btnCancel时出现错误“ AttributeError:'bool'对象没有属性”。这是引发错误(我认为)textboxValue = self.txtCustCode.text
的地方。我什至尝试了textboxValue = self.txtCustCode.text()
以下是代码
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_())
答案 0 :(得分:0)
下面的行返回一个布尔值。
self.txtCustCode = QLineEdit(self.centralwidget)
然后,您尝试使用setGeometry
属性。这就是为什么它会给您错误。
self.txtCustCode.setGeometry(QtCore.QRect(230, 160, 211, 20))
self.txtCustCode.setObjectName("txtCustCode")