无法使 PyQt5 Gui 工作,python3
Can't make PyQt5 Gui work, python3
我用 Qt Designer 制作了几个 guis,然后用 PyQt5 将 .ui 转换为 .py,但是,每当我 运行 此代码在 python3 没有任何反应,没有 gui,没有错误,没有,我是 python 的新手 qui,我不知道为什么会这样。
-- 编码:utf-8 --
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# 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(629, 451)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_3 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_3.addLayout(self.gridLayout, 1, 0, 1, 1)
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.pushButton_4 = QtWidgets.QPushButton(self.tab)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_7.addWidget(self.pushButton_4)
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_7.addWidget(self.pushButton_2)
self.pushButton_15 = QtWidgets.QPushButton(self.tab)
self.pushButton_15.setObjectName("pushButton_15")
self.horizontalLayout_7.addWidget(self.pushButton_15)
self.gridLayout_3.addLayout(self.horizontalLayout_7, 0, 0, 1, 1)
self.tableView = QtWidgets.QTableView(self.tab)
self.tableView.setObjectName("tableView")
self.gridLayout_3.addWidget(self.tableView, 2, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.tab_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
self.horizontalLayout_11.setObjectName("horizontalLayout_11")
self.pushButton_6 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_11.addWidget(self.pushButton_6)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_11.addWidget(self.pushButton_5)
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_11.addWidget(self.pushButton_3)
self.verticalLayout.addLayout(self.horizontalLayout_11)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.tableView_2 = QtWidgets.QTableView(self.tab_2)
self.tableView_2.setObjectName("tableView_2")
self.verticalLayout_3.addWidget(self.tableView_2)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tab_3)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout()
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.pushButton_19 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_19.setObjectName("pushButton_19")
self.horizontalLayout_13.addWidget(self.pushButton_19)
self.pushButton_18 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_18.setObjectName("pushButton_18")
self.horizontalLayout_13.addWidget(self.pushButton_18)
self.pushButton_7 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_7.setObjectName("pushButton_7")
self.horizontalLayout_13.addWidget(self.pushButton_7)
self.verticalLayout_4.addLayout(self.horizontalLayout_13)
self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.verticalLayout_4.addLayout(self.horizontalLayout_12)
self.tableView_3 = QtWidgets.QTableView(self.tab_3)
self.tableView_3.setObjectName("tableView_3")
self.verticalLayout_4.addWidget(self.tableView_3)
self.tabWidget.addTab(self.tab_3, "")
self.horizontalLayout.addWidget(self.tabWidget)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout_5.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_5.addWidget(self.lineEdit)
self.gridLayout_2.addLayout(self.horizontalLayout_5, 1, 0, 1, 1)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_17.setObjectName("pushButton_17")
self.horizontalLayout_6.addWidget(self.pushButton_17)
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setObjectName("pushButton_16")
self.horizontalLayout_6.addWidget(self.pushButton_16)
self.gridLayout_2.addLayout(self.horizontalLayout_6, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 629, 21))
self.menubar.setObjectName("menubar")
self.menu_File = QtWidgets.QMenu(self.menubar)
self.menu_File.setObjectName("menu_File")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuVer = QtWidgets.QMenu(self.menubar)
self.menuVer.setObjectName("menuVer")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menu_File.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuVer.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Agregar profesor"))
self.pushButton_2.setText(_translate("MainWindow", "Borrar profesor"))
self.pushButton_15.setText(_translate("MainWindow", "Editar profesor"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profesores"))
self.pushButton_6.setText(_translate("MainWindow", "Agregar seccion"))
self.pushButton_5.setText(_translate("MainWindow", "Editar seccion"))
self.pushButton_3.setText(_translate("MainWindow", "Borrar seccion"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Secciones"))
self.pushButton_19.setText(_translate("MainWindow", "Agregar clase"))
self.pushButton_18.setText(_translate("MainWindow", "Editar clase"))
self.pushButton_7.setText(_translate("MainWindow", "Borrar clase"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Horarios"))
self.label.setText(_translate("MainWindow", "TextGoesHere"))
self.pushButton_17.setText(_translate("MainWindow", "Aceptar"))
self.pushButton_16.setText(_translate("MainWindow", "Cancelar"))
self.menu_File.setTitle(_translate("MainWindow", "&File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
self.menuVer.setTitle(_translate("MainWindow", "Ver"))
那是因为你有 运行 类似的东西:
pyuic5 view.ui -o view.py
其中 view.ui 是您的 Qt Designer 文件,view.py 是您的输出文件。输出文件 view.py 不可执行。如果您查看文档,您会发现您可以使用另一个标志 (-x) 作为可执行文件。
所以你可以运行:
pyuic5 view.ui -xo view.py
这将创建以下文件:
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# 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(629, 451)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_3 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_3.addLayout(self.gridLayout, 1, 0, 1, 1)
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.pushButton_4 = QtWidgets.QPushButton(self.tab)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_7.addWidget(self.pushButton_4)
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_7.addWidget(self.pushButton_2)
self.pushButton_15 = QtWidgets.QPushButton(self.tab)
self.pushButton_15.setObjectName("pushButton_15")
self.horizontalLayout_7.addWidget(self.pushButton_15)
self.gridLayout_3.addLayout(self.horizontalLayout_7, 0, 0, 1, 1)
self.tableView = QtWidgets.QTableView(self.tab)
self.tableView.setObjectName("tableView")
self.gridLayout_3.addWidget(self.tableView, 2, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.tab_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
self.horizontalLayout_11.setObjectName("horizontalLayout_11")
self.pushButton_6 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_11.addWidget(self.pushButton_6)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_11.addWidget(self.pushButton_5)
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_11.addWidget(self.pushButton_3)
self.verticalLayout.addLayout(self.horizontalLayout_11)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.tableView_2 = QtWidgets.QTableView(self.tab_2)
self.tableView_2.setObjectName("tableView_2")
self.verticalLayout_3.addWidget(self.tableView_2)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tab_3)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout()
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.pushButton_19 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_19.setObjectName("pushButton_19")
self.horizontalLayout_13.addWidget(self.pushButton_19)
self.pushButton_18 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_18.setObjectName("pushButton_18")
self.horizontalLayout_13.addWidget(self.pushButton_18)
self.pushButton_7 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_7.setObjectName("pushButton_7")
self.horizontalLayout_13.addWidget(self.pushButton_7)
self.verticalLayout_4.addLayout(self.horizontalLayout_13)
self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.verticalLayout_4.addLayout(self.horizontalLayout_12)
self.tableView_3 = QtWidgets.QTableView(self.tab_3)
self.tableView_3.setObjectName("tableView_3")
self.verticalLayout_4.addWidget(self.tableView_3)
self.tabWidget.addTab(self.tab_3, "")
self.horizontalLayout.addWidget(self.tabWidget)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout_5.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_5.addWidget(self.lineEdit)
self.gridLayout_2.addLayout(self.horizontalLayout_5, 1, 0, 1, 1)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_17.setObjectName("pushButton_17")
self.horizontalLayout_6.addWidget(self.pushButton_17)
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setObjectName("pushButton_16")
self.horizontalLayout_6.addWidget(self.pushButton_16)
self.gridLayout_2.addLayout(self.horizontalLayout_6, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 629, 21))
self.menubar.setObjectName("menubar")
self.menu_File = QtWidgets.QMenu(self.menubar)
self.menu_File.setObjectName("menu_File")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuVer = QtWidgets.QMenu(self.menubar)
self.menuVer.setObjectName("menuVer")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menu_File.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuVer.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Agregar profesor"))
self.pushButton_2.setText(_translate("MainWindow", "Borrar profesor"))
self.pushButton_15.setText(_translate("MainWindow", "Editar profesor"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profesores"))
self.pushButton_6.setText(_translate("MainWindow", "Agregar seccion"))
self.pushButton_5.setText(_translate("MainWindow", "Editar seccion"))
self.pushButton_3.setText(_translate("MainWindow", "Borrar seccion"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Secciones"))
self.pushButton_19.setText(_translate("MainWindow", "Agregar clase"))
self.pushButton_18.setText(_translate("MainWindow", "Editar clase"))
self.pushButton_7.setText(_translate("MainWindow", "Borrar clase"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Horarios"))
self.label.setText(_translate("MainWindow", "TextGoesHere"))
self.pushButton_17.setText(_translate("MainWindow", "Aceptar"))
self.pushButton_16.setText(_translate("MainWindow", "Cancelar"))
self.menu_File.setTitle(_translate("MainWindow", "&File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
self.menuVer.setTitle(_translate("MainWindow", "Ver"))
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_())
但是,您可能希望将逻辑与表示分开。这就是为什么一旦您使用 pyuic5 命令行工具创建了 .py 文件,您将创建一个名为 app.py 的单独文件说:
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import (QMainWindow, QApplication)
from view import Ui_MainWindow
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
import sys
QCoreApplication.setApplicationName("app name")
QCoreApplication.setApplicationVersion("app version")
QCoreApplication.setOrganizationName("Your name")
QCoreApplication.setOrganizationDomain("Your URL")
app = QApplication(sys.argv)
form = Main()
form.show()
sys.exit(app.exec_())
我用 Qt Designer 制作了几个 guis,然后用 PyQt5 将 .ui 转换为 .py,但是,每当我 运行 此代码在 python3 没有任何反应,没有 gui,没有错误,没有,我是 python 的新手 qui,我不知道为什么会这样。
-- 编码:utf-8 --
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# 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(629, 451)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_3 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_3.addLayout(self.gridLayout, 1, 0, 1, 1)
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.pushButton_4 = QtWidgets.QPushButton(self.tab)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_7.addWidget(self.pushButton_4)
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_7.addWidget(self.pushButton_2)
self.pushButton_15 = QtWidgets.QPushButton(self.tab)
self.pushButton_15.setObjectName("pushButton_15")
self.horizontalLayout_7.addWidget(self.pushButton_15)
self.gridLayout_3.addLayout(self.horizontalLayout_7, 0, 0, 1, 1)
self.tableView = QtWidgets.QTableView(self.tab)
self.tableView.setObjectName("tableView")
self.gridLayout_3.addWidget(self.tableView, 2, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.tab_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
self.horizontalLayout_11.setObjectName("horizontalLayout_11")
self.pushButton_6 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_11.addWidget(self.pushButton_6)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_11.addWidget(self.pushButton_5)
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_11.addWidget(self.pushButton_3)
self.verticalLayout.addLayout(self.horizontalLayout_11)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.tableView_2 = QtWidgets.QTableView(self.tab_2)
self.tableView_2.setObjectName("tableView_2")
self.verticalLayout_3.addWidget(self.tableView_2)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tab_3)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout()
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.pushButton_19 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_19.setObjectName("pushButton_19")
self.horizontalLayout_13.addWidget(self.pushButton_19)
self.pushButton_18 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_18.setObjectName("pushButton_18")
self.horizontalLayout_13.addWidget(self.pushButton_18)
self.pushButton_7 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_7.setObjectName("pushButton_7")
self.horizontalLayout_13.addWidget(self.pushButton_7)
self.verticalLayout_4.addLayout(self.horizontalLayout_13)
self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.verticalLayout_4.addLayout(self.horizontalLayout_12)
self.tableView_3 = QtWidgets.QTableView(self.tab_3)
self.tableView_3.setObjectName("tableView_3")
self.verticalLayout_4.addWidget(self.tableView_3)
self.tabWidget.addTab(self.tab_3, "")
self.horizontalLayout.addWidget(self.tabWidget)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout_5.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_5.addWidget(self.lineEdit)
self.gridLayout_2.addLayout(self.horizontalLayout_5, 1, 0, 1, 1)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_17.setObjectName("pushButton_17")
self.horizontalLayout_6.addWidget(self.pushButton_17)
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setObjectName("pushButton_16")
self.horizontalLayout_6.addWidget(self.pushButton_16)
self.gridLayout_2.addLayout(self.horizontalLayout_6, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 629, 21))
self.menubar.setObjectName("menubar")
self.menu_File = QtWidgets.QMenu(self.menubar)
self.menu_File.setObjectName("menu_File")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuVer = QtWidgets.QMenu(self.menubar)
self.menuVer.setObjectName("menuVer")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menu_File.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuVer.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Agregar profesor"))
self.pushButton_2.setText(_translate("MainWindow", "Borrar profesor"))
self.pushButton_15.setText(_translate("MainWindow", "Editar profesor"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profesores"))
self.pushButton_6.setText(_translate("MainWindow", "Agregar seccion"))
self.pushButton_5.setText(_translate("MainWindow", "Editar seccion"))
self.pushButton_3.setText(_translate("MainWindow", "Borrar seccion"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Secciones"))
self.pushButton_19.setText(_translate("MainWindow", "Agregar clase"))
self.pushButton_18.setText(_translate("MainWindow", "Editar clase"))
self.pushButton_7.setText(_translate("MainWindow", "Borrar clase"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Horarios"))
self.label.setText(_translate("MainWindow", "TextGoesHere"))
self.pushButton_17.setText(_translate("MainWindow", "Aceptar"))
self.pushButton_16.setText(_translate("MainWindow", "Cancelar"))
self.menu_File.setTitle(_translate("MainWindow", "&File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
self.menuVer.setTitle(_translate("MainWindow", "Ver"))
那是因为你有 运行 类似的东西:
pyuic5 view.ui -o view.py
其中 view.ui 是您的 Qt Designer 文件,view.py 是您的输出文件。输出文件 view.py 不可执行。如果您查看文档,您会发现您可以使用另一个标志 (-x) 作为可执行文件。
所以你可以运行:
pyuic5 view.ui -xo view.py
这将创建以下文件:
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# 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(629, 451)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_3 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_3.addLayout(self.gridLayout, 1, 0, 1, 1)
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.pushButton_4 = QtWidgets.QPushButton(self.tab)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_7.addWidget(self.pushButton_4)
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_7.addWidget(self.pushButton_2)
self.pushButton_15 = QtWidgets.QPushButton(self.tab)
self.pushButton_15.setObjectName("pushButton_15")
self.horizontalLayout_7.addWidget(self.pushButton_15)
self.gridLayout_3.addLayout(self.horizontalLayout_7, 0, 0, 1, 1)
self.tableView = QtWidgets.QTableView(self.tab)
self.tableView.setObjectName("tableView")
self.gridLayout_3.addWidget(self.tableView, 2, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.tab_2)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_11 = QtWidgets.QHBoxLayout()
self.horizontalLayout_11.setObjectName("horizontalLayout_11")
self.pushButton_6 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_11.addWidget(self.pushButton_6)
self.pushButton_5 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_11.addWidget(self.pushButton_5)
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_11.addWidget(self.pushButton_3)
self.verticalLayout.addLayout(self.horizontalLayout_11)
self.verticalLayout_3.addLayout(self.verticalLayout)
self.tableView_2 = QtWidgets.QTableView(self.tab_2)
self.tableView_2.setObjectName("tableView_2")
self.verticalLayout_3.addWidget(self.tableView_2)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.tab_3)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout()
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.pushButton_19 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_19.setObjectName("pushButton_19")
self.horizontalLayout_13.addWidget(self.pushButton_19)
self.pushButton_18 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_18.setObjectName("pushButton_18")
self.horizontalLayout_13.addWidget(self.pushButton_18)
self.pushButton_7 = QtWidgets.QPushButton(self.tab_3)
self.pushButton_7.setObjectName("pushButton_7")
self.horizontalLayout_13.addWidget(self.pushButton_7)
self.verticalLayout_4.addLayout(self.horizontalLayout_13)
self.horizontalLayout_12 = QtWidgets.QHBoxLayout()
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.verticalLayout_4.addLayout(self.horizontalLayout_12)
self.tableView_3 = QtWidgets.QTableView(self.tab_3)
self.tableView_3.setObjectName("tableView_3")
self.verticalLayout_4.addWidget(self.tableView_3)
self.tabWidget.addTab(self.tab_3, "")
self.horizontalLayout.addWidget(self.tabWidget)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout_5.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_5.addWidget(self.lineEdit)
self.gridLayout_2.addLayout(self.horizontalLayout_5, 1, 0, 1, 1)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_17.setObjectName("pushButton_17")
self.horizontalLayout_6.addWidget(self.pushButton_17)
self.pushButton_16 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_16.setObjectName("pushButton_16")
self.horizontalLayout_6.addWidget(self.pushButton_16)
self.gridLayout_2.addLayout(self.horizontalLayout_6, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 629, 21))
self.menubar.setObjectName("menubar")
self.menu_File = QtWidgets.QMenu(self.menubar)
self.menu_File.setObjectName("menu_File")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuVer = QtWidgets.QMenu(self.menubar)
self.menuVer.setObjectName("menuVer")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menu_File.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuVer.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "Agregar profesor"))
self.pushButton_2.setText(_translate("MainWindow", "Borrar profesor"))
self.pushButton_15.setText(_translate("MainWindow", "Editar profesor"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Profesores"))
self.pushButton_6.setText(_translate("MainWindow", "Agregar seccion"))
self.pushButton_5.setText(_translate("MainWindow", "Editar seccion"))
self.pushButton_3.setText(_translate("MainWindow", "Borrar seccion"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Secciones"))
self.pushButton_19.setText(_translate("MainWindow", "Agregar clase"))
self.pushButton_18.setText(_translate("MainWindow", "Editar clase"))
self.pushButton_7.setText(_translate("MainWindow", "Borrar clase"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Horarios"))
self.label.setText(_translate("MainWindow", "TextGoesHere"))
self.pushButton_17.setText(_translate("MainWindow", "Aceptar"))
self.pushButton_16.setText(_translate("MainWindow", "Cancelar"))
self.menu_File.setTitle(_translate("MainWindow", "&File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
self.menuVer.setTitle(_translate("MainWindow", "Ver"))
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_())
但是,您可能希望将逻辑与表示分开。这就是为什么一旦您使用 pyuic5 命令行工具创建了 .py 文件,您将创建一个名为 app.py 的单独文件说:
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import (QMainWindow, QApplication)
from view import Ui_MainWindow
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
import sys
QCoreApplication.setApplicationName("app name")
QCoreApplication.setApplicationVersion("app version")
QCoreApplication.setOrganizationName("Your name")
QCoreApplication.setOrganizationDomain("Your URL")
app = QApplication(sys.argv)
form = Main()
form.show()
sys.exit(app.exec_())