如果我运行我的代码,则会出现错误!我在Qt Designer中进行了设计,并在布局中使用了grid函数,但出现错误。我使用了不同的代码,但是没有用。我使用Pycharm进行编码和运行代码。
请帮助我!
Pyhton ProjectEng.pyw-布局
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ProjectEng.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(655, 665)
self.Grid = QtWidgets.QWidget(MainWindow)
self.Grid.setEnabled(True)
self.Grid.setAcceptDrops(False)
self.Grid.setAutoFillBackground(False)
self.Grid.setObjectName("Grid")
self.gridLayout_2 = QtWidgets.QGridLayout(self.Grid)
self.gridLayout_2.setObjectName("gridLayout_2")
self.Start = QtWidgets.QPushButton(self.Grid)
self.Start.setEnabled(True)
font = QtGui.QFont()
font.setPointSize(15)
self.Start.setFont(font)
self.Start.setStyleSheet("background-color: rgb(85, 255, 0);\n"
"border-radius: 4px;\n"
"height: 100px;")
self.Start.setObjectName("Start")
self.gridLayout_2.addWidget(self.Start, 0, 1, 1, 1)
self.Klok = QtWidgets.QLCDNumber(self.Grid)
self.Klok.setStyleSheet("height: 200px;")
self.Klok.setObjectName("Klok")
self.gridLayout_2.addWidget(self.Klok, 0, 0, 2, 1)
self.Stop = QtWidgets.QPushButton(self.Grid)
font = QtGui.QFont()
font.setPointSize(15)
self.Stop.setFont(font)
self.Stop.setStyleSheet("background-color: rgb(255, 0, 0);\n"
"border-radius: 4px;\n"
"height: 100px;")
self.Stop.setObjectName("Stop")
self.gridLayout_2.addWidget(self.Stop, 1, 1, 1, 1)
self.Metalen = QtWidgets.QLabel(self.Grid)
self.Metalen.setMaximumSize(QtCore.QSize(800000, 45))
font = QtGui.QFont()
font.setPointSize(20)
self.Metalen.setFont(font)
self.Metalen.setStyleSheet("")
self.Metalen.setObjectName("Metalen")
self.gridLayout_2.addWidget(self.Metalen, 6, 0, 1, 1)
self.CounterMetalen = QtWidgets.QLCDNumber(self.Grid)
self.CounterMetalen.setStyleSheet("height: 200px;")
self.CounterMetalen.setObjectName("CounterMetalen")
self.gridLayout_2.addWidget(self.CounterMetalen, 8, 0, 1, 1)
self.CounterNietMetalen = QtWidgets.QLCDNumber(self.Grid)
self.CounterNietMetalen.setStyleSheet("height: 200px;")
self.CounterNietMetalen.setObjectName("CounterNietMetalen")
self.gridLayout_2.addWidget(self.CounterNietMetalen, 8, 1, 1, 1)
self.NietMetalen = QtWidgets.QLabel(self.Grid)
self.NietMetalen.setMaximumSize(QtCore.QSize(16777215, 45))
font = QtGui.QFont()
font.setPointSize(20)
self.NietMetalen.setFont(font)
self.NietMetalen.setAutoFillBackground(False)
self.NietMetalen.setStyleSheet("")
self.NietMetalen.setObjectName("NietMetalen")
self.gridLayout_2.addWidget(self.NietMetalen, 6, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(634, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
self.gridLayout_2.addItem(spacerItem, 9, 0, 1, 2)
spacerItem1 = QtWidgets.QSpacerItem(634, 22, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
self.gridLayout_2.addItem(spacerItem1, 4, 0, 1, 2)
self.MetalenPlus = QtWidgets.QPushButton(self.Grid)
self.MetalenPlus.setEnabled(True)
font = QtGui.QFont()
font.setPointSize(15)
self.MetalenPlus.setFont(font)
self.MetalenPlus.setStyleSheet("border-radius: 4px;\n"
"background-color: rgb(223, 223, 223);\n"
"height: 50px;")
self.MetalenPlus.setObjectName("MetalenPlus")
self.gridLayout_2.addWidget(self.MetalenPlus, 10, 0, 1, 1)
self.MetalenMin = QtWidgets.QPushButton(self.Grid)
font = QtGui.QFont()
font.setPointSize(15)
self.MetalenMin.setFont(font)
self.MetalenMin.setStyleSheet("border-radius: 4px;\n"
"background-color: rgb(223, 223, 223);\n"
"height: 50px;")
self.MetalenMin.setObjectName("MetalenMin")
self.gridLayout_2.addWidget(self.MetalenMin, 13, 0, 1, 1)
self.Reset = QtWidgets.QPushButton(self.Grid)
self.Reset.setEnabled(True)
font = QtGui.QFont()
font.setPointSize(15)
self.Reset.setFont(font)
self.Reset.setStyleSheet("border-radius: 4px;\n"
"background-color: rgb(223, 223, 223);\n"
"height: 50px;")
self.Reset.setObjectName("Reset")
self.gridLayout_2.addWidget(self.Reset, 18, 0, 1, 2)
spacerItem2 = QtWidgets.QSpacerItem(634, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
self.gridLayout_2.addItem(spacerItem2, 17, 0, 1, 2)
self.NietMetalenMin = QtWidgets.QPushButton(self.Grid)
font = QtGui.QFont()
font.setPointSize(15)
self.NietMetalenMin.setFont(font)
self.NietMetalenMin.setStyleSheet("border-radius: 4px;\n"
"background-color: rgb(223, 223, 223);\n"
"height: 50px;")
self.NietMetalenMin.setObjectName("NietMetalenMin")
self.gridLayout_2.addWidget(self.NietMetalenMin, 13, 1, 1, 1)
self.NietMetalenPlus = QtWidgets.QPushButton(self.Grid)
self.NietMetalenPlus.setEnabled(True)
font = QtGui.QFont()
font.setPointSize(15)
self.NietMetalenPlus.setFont(font)
self.NietMetalenPlus.setStyleSheet("border-radius: 4px;\n"
"background-color: rgb(223, 223, 223);\n"
"height: 50px;")
self.NietMetalenPlus.setObjectName("NietMetalenPlus")
self.gridLayout_2.addWidget(self.NietMetalenPlus, 10, 1, 1, 1)
MainWindow.setCentralWidget(self.Grid)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 655, 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", "Project Xander en Tom"))
self.Start.setText(_translate("MainWindow", "Start"))
self.Stop.setText(_translate("MainWindow", "Stop"))
self.Metalen.setText(_translate("MainWindow", "Metalen"))
self.NietMetalen.setText(_translate("MainWindow", "Niet Metalen"))
self.MetalenPlus.setText(_translate("MainWindow", "Metalen plus"))
self.MetalenMin.setText(_translate("MainWindow", "Metalen min"))
self.Reset.setText(_translate("MainWindow", "Reset"))
self.NietMetalenMin.setText(_translate("MainWindow", "Niet Metalen min"))
self.NietMetalenPlus.setText(_translate("MainWindow", "Niet Metalen plus"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Python ProjectEngCode.py-时钟代码
from PyQt5 import QtWidgets, QtCore
import ProjectEng
import sys
class ContLCDClock(QtWidgets.QWidget):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = ProjectEng.Ui_MainWindow()
self.ui.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateLCD)
self.timer.start(1000)
def updateLCD(self):
self.currentTime = QtCore.QTime.currentTime()
self.strCurrentTime = self.currentTime.toString('hh:mm')
self.ui.Klok.display(self.strCurrentTime)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
c = ContLCDClock()
c.show()
sys.exit(app.exec_())
错误-程序PyCharm
Traceback (most recent call last):
File "D:/Documenten/Python ui/Eng/ProjectEngCode.py", line 22, in <module>
c = ContLCDClock()
File "D:/Documenten/Python ui/Eng/ProjectEngCode.py", line 9, in __init__
self.ui.setupUi(self)
File "D:\Documenten\Python ui\Eng\ProjectEng.pyw", line 124, in setupUi
MainWindow.setCentralWidget(self.Grid)
AttributeError: 'ContLCDClock' object has no attribute 'setCentralWidget'