Python 提供了很多个UI制作的库,例如Tkinter、wxPython、PyGTK、PySide、PyQt等等。那小Y君今天主要介绍的就是利用QTDesigner已有设计的界面快速生成py文件。由于小Y君是一个很懒的人,所以就选择了这个懒人制作UI界面的方式。使用的Python库就是PyQt5。那小Y君就不啰嗦其他的了,直奔主题(->. <-)
小Y君使用的环境
:
Ubuntu 18.04.1 LTS
QT Designer 4.8.7
Pycharm Community 2018.2
(Python 3.6 PyQt5)
注:如不知道怎么安装PyQt5或者其他需要使用的环境,请回复留言即可。
Qt Designer 界面
Qt Designer是直接用来做QT程序界面的,使用起来也比较简单,易上手,使用方式就是拖拽需要材料至主界面上即可。
下图是小编简单制作的一个界面
这个界面很简单了,一个显示当前配置的文字窗口,加上三个按钮,增加,修改,和删除配置项的按钮,点击Add按钮跳转到增加配合项页面,进行设定。
设计好界面呢,下一步就是保存该ui文件
将Qt Designer 所生成的ui文件转换成Python脚本
转换的指令非常简单
pyuic5 -o mainpage.py /home/ubuntu1710/py_GUI/mainpage.ui
-o 后的参数为输出文件的名称
-o 后第二个参数即为生成的ui文件的名称
生成的code 如下所示
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '/home/ubuntu1710/py_GUI/untitled.ui' # # Created by: PyQt5 UI code generator 5.10.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(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setMouseTracking(False) self.centralwidget.setObjectName("centralwidget") self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) self.textBrowser.setGeometry(QtCore.QRect(30, 40, 751, 221)) self.textBrowser.setObjectName("textBrowser") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(30, 20, 191, 17)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(40, 306, 171, 31)) self.label_2.setObjectName("label_2") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(40, 350, 171, 27)) self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(40, 420, 171, 27)) self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(40, 490, 171, 31)) self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton_3.setObjectName("pushButton_3") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(230, 350, 531, 41)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(230, 420, 531, 41)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(230, 490, 531, 41)) self.label_5.setObjectName("label_5") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) 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", "Current Configuration")) self.label_2.setText(_translate("MainWindow", "Configuration Wizard")) self.pushButton.setText(_translate("MainWindow", "Add New Config")) self.pushButton_2.setText(_translate("MainWindow", "Modify Config")) self.pushButton_3.setText(_translate("MainWindow", "Delete Config")) self.label_3.setText(_translate("MainWindow", "Add new configuration")) self.label_4.setText(_translate("MainWindow", "Modify Configuration")) self.label_5.setText(_translate("MainWindow", "Delete Configuration"))
使用pycharm创建工程,新建一个main.py文件
创建main.py 文件,调用代码如下
# -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication , QMainWindow from page import * if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(mainWindow) mainWindow.show() sys.exit(app.exec_())
执行起来,跟QT Designer 是一模一样的
点击Add config 按钮,弹出设置页面
重复使用Qt Designer制作Ui界面,在使用pyuic5生成py脚本
在刚才的主界面code中增加代码
将制作的add 页面生成的code addPage.py 加入mainpage.py中
import addPage
mainpage.py-> setupUi中增加红色字体Highlight部分
class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.mainwindows = MainWindow
mainpage.py-> retranslateUi中增加红色字体Highlight部分
def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "Current Configuration")) self.label_2.setText(_translate("MainWindow", "Configuration Wizard")) self.pushButton.setText(_translate("MainWindow", "Add New Config")) self.pushButton_2.setText(_translate("MainWindow", "Modify Config")) self.pushButton_3.setText(_translate("MainWindow", "Delete Config")) self.label_3.setText(_translate("MainWindow", "Add new configuration")) self.label_4.setText(_translate("MainWindow", "Modify Configuration")) self.label_5.setText(_translate("MainWindow", "Delete Configuration")) self.pushButton.clicked.connect(self.addClicked)
mainpage.py-> class Ui_MainWindow中addClicked
def addClicked(self): self.mainwindows.hide() page_add = QtWidgets.QDialog() ui_add = addPage.AddDlg() ui_add.setupUi(page_add) page_add.show() page_add.exec_() self.mainwindows.show()
再次运行Python脚本main.py
点击Add new config Button,将弹出如下图
mainpage.py完整代码
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '/home/ubuntu1710/py_GUI/untitled.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets import addPage class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.mainwindows = MainWindow self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setMouseTracking(False) self.centralwidget.setObjectName("centralwidget") self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) self.textBrowser.setGeometry(QtCore.QRect(30, 40, 751, 221)) self.textBrowser.setObjectName("textBrowser") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(30, 20, 191, 17)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(40, 306, 171, 31)) self.label_2.setObjectName("label_2") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(40, 350, 171, 27)) self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(40, 420, 171, 27)) self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(40, 490, 171, 31)) self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) self.pushButton_3.setObjectName("pushButton_3") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(230, 350, 531, 41)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(230, 420, 531, 41)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(230, 490, 531, 41)) self.label_5.setObjectName("label_5") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) 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", "Current Configuration")) self.label_2.setText(_translate("MainWindow", "Configuration Wizard")) self.pushButton.setText(_translate("MainWindow", "Add New Config")) self.pushButton_2.setText(_translate("MainWindow", "Modify Config")) self.pushButton_3.setText(_translate("MainWindow", "Delete Config")) self.label_3.setText(_translate("MainWindow", "Add new configuration")) self.label_4.setText(_translate("MainWindow", "Modify Configuration")) self.label_5.setText(_translate("MainWindow", "Delete Configuration")) self.pushButton.clicked.connect(self.addClicked) def addClicked(self): self.mainwindows.hide() page_add = QtWidgets.QDialog() ui_add = addPage.AddDlg() ui_add.setupUi(page_add) page_add.show() page_add.exec_() self.mainwindows.show()
喜欢的亲们,扫一扫加个关注