i've made several guis with Qt Designer and then with PyQt5 i converted the .ui to .py, however, whenever i run this code on python3 nothing happens, no gui, no errors, nothing, i'm quite new to python and i have no idea why this is happening.
我用Qt Designer制作了几个gui然后用PyQt5我将.ui转换为.py,但是,每当我在python3上运行这个代码时没有任何反应,没有gui,没有错误,什么都没有,我对python和我很新我不知道为什么会这样。
-- coding: 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"))
1 个解决方案
#1
That is becuase you have ran something along the lines of:
那是因为你已经按照以下方式运行:
pyuic5 view.ui -o view.py
Where view.ui is your Qt Designer file and view.py is your output file. The output file view.py isn't executable. If you look at the documentation, you will see that you can use another flag (-x) for executable.
其中view.ui是您的Qt Designer文件,view.py是您的输出文件。输出文件view.py不可执行。如果查看文档,您将看到可以使用另一个标志(-x)表示可执行文件。
So you can run:
所以你可以运行:
pyuic5 view.ui -xo view.py
This will create the following file:
这将创建以下文件:
# 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_())
However, you would want to keep your logic separate from the presentation. That is why once you have created the .py file using the pyuic5 command line tool you would create a separate file called app.py say:
但是,您希望将逻辑与演示文稿分开。这就是为什么一旦你使用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_())
#1
That is becuase you have ran something along the lines of:
那是因为你已经按照以下方式运行:
pyuic5 view.ui -o view.py
Where view.ui is your Qt Designer file and view.py is your output file. The output file view.py isn't executable. If you look at the documentation, you will see that you can use another flag (-x) for executable.
其中view.ui是您的Qt Designer文件,view.py是您的输出文件。输出文件view.py不可执行。如果查看文档,您将看到可以使用另一个标志(-x)表示可执行文件。
So you can run:
所以你可以运行:
pyuic5 view.ui -xo view.py
This will create the following file:
这将创建以下文件:
# 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_())
However, you would want to keep your logic separate from the presentation. That is why once you have created the .py file using the pyuic5 command line tool you would create a separate file called app.py say:
但是,您希望将逻辑与演示文稿分开。这就是为什么一旦你使用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_())