我在Qt Creator中为我的python应用程序创建了一个ui,并使用pyuic5 mainwindow.ui -o mainwindow.py
将其从.ui转换为.py,但是当我运行mainwindow.py时,没有窗口出现。我在macOS上,但我也在我的Windows PC上试过它,它也没有在那里工作。这是我的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(731, 466)
MainWindow.setAnimated(True)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout_4 = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout_4.setContentsMargins(11, 11, 11, 11)
self.gridLayout_4.setSpacing(6)
self.gridLayout_4.setObjectName("gridLayout_4")
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setObjectName("label")
self.gridLayout_4.addWidget(self.label, 0, 0, 1, 1)
self.username = QtWidgets.QLineEdit(self.centralWidget)
self.username.setObjectName("username")
self.gridLayout_4.addWidget(self.username, 0, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(508, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_4.addItem(spacerItem, 0, 2, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralWidget)
self.label_2.setObjectName("label_2")
self.gridLayout_4.addWidget(self.label_2, 1, 0, 1, 1)
self.password = QtWidgets.QLineEdit(self.centralWidget)
self.password.setObjectName("password")
self.gridLayout_4.addWidget(self.password, 1, 1, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(508, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_4.addItem(spacerItem1, 1, 2, 1, 1)
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setContentsMargins(11, 11, 11, 11)
self.gridLayout_3.setSpacing(6)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
self.gridLayout_2.setSpacing(6)
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_16 = QtWidgets.QLabel(self.centralWidget)
self.label_16.setObjectName("label_16")
self.gridLayout_2.addWidget(self.label_16, 3, 4, 1, 1)
self.label_17 = QtWidgets.QLabel(self.centralWidget)
self.label_17.setObjectName("label_17")
self.gridLayout_2.addWidget(self.label_17, 4, 4, 1, 1)
self.label_5 = QtWidgets.QLabel(self.centralWidget)
self.label_5.setObjectName("label_5")
self.gridLayout_2.addWidget(self.label_5, 2, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralWidget)
self.label_4.setObjectName("label_4")
self.gridLayout_2.addWidget(self.label_4, 1, 0, 1, 1)
self.label_11 = QtWidgets.QLabel(self.centralWidget)
self.label_11.setObjectName("label_11")
self.gridLayout_2.addWidget(self.label_11, 3, 2, 1, 1)
self.u6 = QtWidgets.QLineEdit(self.centralWidget)
self.u6.setObjectName("u6")
self.gridLayout_2.addWidget(self.u6, 0, 3, 1, 1)
self.u1 = QtWidgets.QLineEdit(self.centralWidget)
self.u1.setObjectName("u1")
self.gridLayout_2.addWidget(self.u1, 0, 1, 1, 1)
self.label_13 = QtWidgets.QLabel(self.centralWidget)
self.label_13.setObjectName("label_13")
self.gridLayout_2.addWidget(self.label_13, 0, 4, 1, 1)
self.label_14 = QtWidgets.QLabel(self.centralWidget)
self.label_14.setObjectName("label_14")
self.gridLayout_2.addWidget(self.label_14, 1, 4, 1, 1)
self.label_9 = QtWidgets.QLabel(self.centralWidget)
self.label_9.setObjectName("label_9")
self.gridLayout_2.addWidget(self.label_9, 1, 2, 1, 1)
self.label_18 = QtWidgets.QLabel(self.centralWidget)
self.label_18.setObjectName("label_18")
self.gridLayout_2.addWidget(self.label_18, 1, 6, 1, 1)
self.label_12 = QtWidgets.QLabel(self.centralWidget)
self.label_12.setObjectName("label_12")
self.gridLayout_2.addWidget(self.label_12, 4, 2, 1, 1)
self.u15 = QtWidgets.QLineEdit(self.centralWidget)
self.u15.setObjectName("u15")
self.gridLayout_2.addWidget(self.u15, 4, 5, 1, 1)
self.u18 = QtWidgets.QLineEdit(self.centralWidget)
self.u18.setObjectName("u18")
self.gridLayout_2.addWidget(self.u18, 2, 7, 1, 1)
self.u9 = QtWidgets.QLineEdit(self.centralWidget)
self.u9.setObjectName("u9")
self.gridLayout_2.addWidget(self.u9, 3, 3, 1, 1)
self.u17 = QtWidgets.QLineEdit(self.centralWidget)
self.u17.setObjectName("u17")
self.gridLayout_2.addWidget(self.u17, 1, 7, 1, 1)
self.label_22 = QtWidgets.QLabel(self.centralWidget)
self.label_22.setObjectName("label_22")
self.gridLayout_2.addWidget(self.label_22, 3, 6, 1, 1)
self.label_20 = QtWidgets.QLabel(self.centralWidget)
self.label_20.setObjectName("label_20")
self.gridLayout_2.addWidget(self.label_20, 2, 6, 1, 1)
self.label_15 = QtWidgets.QLabel(self.centralWidget)
self.label_15.setObjectName("label_15")
self.gridLayout_2.addWidget(self.label_15, 2, 4, 1, 1)
self.u10 = QtWidgets.QLineEdit(self.centralWidget)
self.u10.setObjectName("u10")
self.gridLayout_2.addWidget(self.u10, 4, 3, 1, 1)
self.label_19 = QtWidgets.QLabel(self.centralWidget)
self.label_19.setObjectName("label_19")
self.gridLayout_2.addWidget(self.label_19, 0, 6, 1, 1)
self.u19 = QtWidgets.QLineEdit(self.centralWidget)
self.u19.setObjectName("u19")
self.gridLayout_2.addWidget(self.u19, 3, 7, 1, 1)
self.u14 = QtWidgets.QLineEdit(self.centralWidget)
self.u14.setObjectName("u14")
self.gridLayout_2.addWidget(self.u14, 3, 5, 1, 1)
self.label_8 = QtWidgets.QLabel(self.centralWidget)
self.label_8.setObjectName("label_8")
self.gridLayout_2.addWidget(self.label_8, 0, 2, 1, 1)
self.u2 = QtWidgets.QLineEdit(self.centralWidget)
self.u2.setObjectName("u2")
self.gridLayout_2.addWidget(self.u2, 1, 1, 1, 1)
self.label_7 = QtWidgets.QLabel(self.centralWidget)
self.label_7.setObjectName("label_7")
self.gridLayout_2.addWidget(self.label_7, 4, 0, 1, 1)
self.u20 = QtWidgets.QLineEdit(self.centralWidget)
self.u20.setObjectName("u20")
self.gridLayout_2.addWidget(self.u20, 4, 7, 1, 1)
self.u5 = QtWidgets.QLineEdit(self.centralWidget)
self.u5.setObjectName("u5")
self.gridLayout_2.addWidget(self.u5, 4, 1, 1, 1)
self.u4 = QtWidgets.QLineEdit(self.centralWidget)
self.u4.setObjectName("u4")
self.gridLayout_2.addWidget(self.u4, 3, 1, 1, 1)
self.label_6 = QtWidgets.QLabel(self.centralWidget)
self.label_6.setObjectName("label_6")
self.gridLayout_2.addWidget(self.label_6, 3, 0, 1, 1)
self.u13 = QtWidgets.QLineEdit(self.centralWidget)
self.u13.setObjectName("u13")
self.gridLayout_2.addWidget(self.u13, 2, 5, 1, 1)
self.label_10 = QtWidgets.QLabel(self.centralWidget)
self.label_10.setObjectName("label_10")
self.gridLayout_2.addWidget(self.label_10, 2, 2, 1, 1)
self.u11 = QtWidgets.QLineEdit(self.centralWidget)
self.u11.setObjectName("u11")
self.gridLayout_2.addWidget(self.u11, 0, 5, 1, 1)
self.u12 = QtWidgets.QLineEdit(self.centralWidget)
self.u12.setObjectName("u12")
self.gridLayout_2.addWidget(self.u12, 1, 5, 1, 1)
self.label_3 = QtWidgets.QLabel(self.centralWidget)
self.label_3.setObjectName("label_3")
self.gridLayout_2.addWidget(self.label_3, 0, 0, 1, 1)
self.u8 = QtWidgets.QLineEdit(self.centralWidget)
self.u8.setObjectName("u8")
self.gridLayout_2.addWidget(self.u8, 2, 3, 1, 1)
self.u16 = QtWidgets.QLineEdit(self.centralWidget)
self.u16.setObjectName("u16")
self.gridLayout_2.addWidget(self.u16, 0, 7, 1, 1)
self.u7 = QtWidgets.QLineEdit(self.centralWidget)
self.u7.setObjectName("u7")
self.gridLayout_2.addWidget(self.u7, 1, 3, 1, 1)
self.label_21 = QtWidgets.QLabel(self.centralWidget)
self.label_21.setObjectName("label_21")
self.gridLayout_2.addWidget(self.label_21, 4, 6, 1, 1)
self.u3 = QtWidgets.QLineEdit(self.centralWidget)
self.u3.setObjectName("u3")
self.gridLayout_2.addWidget(self.u3, 2, 1, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
self.run = QtWidgets.QPushButton(self.centralWidget)
self.run.setObjectName("run")
self.gridLayout_3.addWidget(self.run, 1, 0, 1, 1)
self.gridLayout_4.addLayout(self.gridLayout_3, 2, 0, 1, 3)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 731, 22))
self.menuBar.setObjectName("menuBar")
self.menuFile = QtWidgets.QMenu(self.menuBar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionExit)
self.menuBar.addAction(self.menuFile.menuAction())
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", "U:"))
self.label_2.setText(_translate("MainWindow", "P:"))
self.label_16.setText(_translate("MainWindow", "14."))
self.label_17.setText(_translate("MainWindow", "15."))
self.label_5.setText(_translate("MainWindow", "3."))
self.label_4.setText(_translate("MainWindow", "2."))
self.label_11.setText(_translate("MainWindow", "9."))
self.label_13.setText(_translate("MainWindow", "11."))
self.label_14.setText(_translate("MainWindow", "12."))
self.label_9.setText(_translate("MainWindow", "7."))
self.label_18.setText(_translate("MainWindow", "17."))
self.label_12.setText(_translate("MainWindow", "10."))
self.label_22.setText(_translate("MainWindow", "19."))
self.label_20.setText(_translate("MainWindow", "18."))
self.label_15.setText(_translate("MainWindow", "13."))
self.label_19.setText(_translate("MainWindow", "16."))
self.label_8.setText(_translate("MainWindow", "6."))
self.label_7.setText(_translate("MainWindow", "5."))
self.label_6.setText(_translate("MainWindow", "4."))
self.label_10.setText(_translate("MainWindow", "8."))
self.label_3.setText(_translate("MainWindow", "1."))
self.label_21.setText(_translate("MainWindow", "20."))
self.run.setText(_translate("MainWindow", "Run"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
答案 0 :(得分:0)
仅uic
"编译"将.ui
XML文件放入一个类中,该类可用于在QMainWindow
上重新创建设计的UI。要使用它,您需要一个使用它的脚本。例如:
#!/usr/bin/python3
import sys
import PyQt5.Qt as Qt
import mainwindow
class MainWindow(Qt.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ui = mainwindow.Ui_MainWindow()
self._ui.setupUi(self)
app = Qt.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()