实现界面开发,与tkinter功能一致,网上已有详细资料,此处仅记录自己的代码:
文章目录
- 1. 实操
- 1.1 main.py
- 1.2. 窗体模块代码
- 1.3. 页面效果
- 2. 参考资料
- 2.1. PyQt5 参考资料
- 2.2. tkinter 参考资料
- 3. 安装注意事项
- 3.1. 下载
- 3.2 Pycharm添加External Tools
1. 实操
实现功能:读取.h文件,读取结构体、枚举、函数信息,在word文档中按照相应格式进行生成。此处仅实现界面相关的功能,具体功能待添加。
word相关操作见:
Python docx学习笔记
1.1 main.py
from testQtui import Ui_MainWindow
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
import ospaths=[] # paths[0]:Module.h文件路径, paths[1]:Module_hw.h文件路径
def addModulePath(self):fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(None, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")paths.insert(0,fileName)ui.inputModuleLineEdit.setText(fileName)print(paths)def addModule_HwPath(self):fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(None, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")paths.insert(1,fileName)ui.inputModulel_HwLineEdit.setText(fileName)print(paths)def StartGen(self):print(ui.comboBox.currentText()) # 获取下拉列表模块当前的选项for i in range(1, 50): # 打印log信息ui.logBrowser.append('test'+ str(i))ui.logBrowser.ensureCursorVisible()ui.OutputFilePathLineEdit.setText('xxxx') # 输出生成文件的路径if __name__ == '__main__':app = QApplication(sys.argv)MainWindow = QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)# MainWindow.setWindowTitle("文档生成") # 设置页面标题,目前在节目中已经设置MainWindow.show()ui.addModuleButton.clicked.connect(addModulePath) # addModuleButton 按键按下实践关联到addModulePath函数ui.addModule_HwButton.clicked.connect(addModule_HwPath)ui.StartGen.clicked.connect(StartGen)sys.exit(app.exec_())
1.2. 窗体模块代码
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'testQtui.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(520, 402)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.inputModuleLineEdit = QtWidgets.QLineEdit(self.centralwidget)self.inputModuleLineEdit.setGeometry(QtCore.QRect(140, 50, 261, 20))self.inputModuleLineEdit.setFocusPolicy(QtCore.Qt.StrongFocus)self.inputModuleLineEdit.setObjectName("inputModuleLineEdit")self.addModuleButton = QtWidgets.QPushButton(self.centralwidget)self.addModuleButton.setGeometry(QtCore.QRect(410, 50, 75, 23))self.addModuleButton.setObjectName("addModuleButton")self.logBrowser = QtWidgets.QTextBrowser(self.centralwidget)self.logBrowser.setGeometry(QtCore.QRect(140, 120, 261, 171))self.logBrowser.setObjectName("logBrowser")self.label = QtWidgets.QLabel(self.centralwidget)self.label.setGeometry(QtCore.QRect(23, 51, 81, 16))font = QtGui.QFont()font.setPointSize(12)self.label.setFont(font)self.label.setLineWidth(1)self.label.setObjectName("label")self.label_2 = QtWidgets.QLabel(self.centralwidget)self.label_2.setGeometry(QtCore.QRect(20, 120, 91, 16))font = QtGui.QFont()font.setPointSize(12)self.label_2.setFont(font)self.label_2.setLineWidth(1)self.label_2.setObjectName("label_2")self.addModule_HwButton = QtWidgets.QPushButton(self.centralwidget)self.addModule_HwButton.setGeometry(QtCore.QRect(410, 80, 75, 23))self.addModule_HwButton.setObjectName("addModule_HwButton")self.inputModulel_HwLineEdit = QtWidgets.QLineEdit(self.centralwidget)self.inputModulel_HwLineEdit.setGeometry(QtCore.QRect(140, 80, 261, 20))self.inputModulel_HwLineEdit.setObjectName("inputModulel_HwLineEdit")self.label_3 = QtWidgets.QLabel(self.centralwidget)self.label_3.setGeometry(QtCore.QRect(23, 82, 91, 16))font = QtGui.QFont()font.setPointSize(12)self.label_3.setFont(font)self.label_3.setLineWidth(1)self.label_3.setObjectName("label_3")self.label_4 = QtWidgets.QLabel(self.centralwidget)self.label_4.setGeometry(QtCore.QRect(23, 22, 80, 16))font = QtGui.QFont()font.setPointSize(12)self.label_4.setFont(font)self.label_4.setLineWidth(1)self.label_4.setObjectName("label_4")self.label_5 = QtWidgets.QLabel(self.centralwidget)self.label_5.setGeometry(QtCore.QRect(10, 330, 381, 21))font = QtGui.QFont()font.setPointSize(12)self.label_5.setFont(font)self.label_5.setLineWidth(1)self.label_5.setObjectName("label_5")self.StartGen = QtWidgets.QPushButton(self.centralwidget)self.StartGen.setGeometry(QtCore.QRect(410, 120, 75, 23))self.StartGen.setObjectName("StartGen")self.label_6 = QtWidgets.QLabel(self.centralwidget)self.label_6.setGeometry(QtCore.QRect(20, 300, 91, 16))font = QtGui.QFont()font.setPointSize(12)self.label_6.setFont(font)self.label_6.setLineWidth(1)self.label_6.setObjectName("label_6")self.OutputFilePathLineEdit = QtWidgets.QLineEdit(self.centralwidget)self.OutputFilePathLineEdit.setGeometry(QtCore.QRect(140, 300, 261, 20))self.OutputFilePathLineEdit.setFocusPolicy(QtCore.Qt.StrongFocus)self.OutputFilePathLineEdit.setObjectName("OutputFilePathLineEdit")self.comboBox = QtWidgets.QComboBox(self.centralwidget)self.comboBox.setGeometry(QtCore.QRect(140, 20, 86, 21))font = QtGui.QFont()font.setPointSize(11)self.comboBox.setFont(font)self.comboBox.setObjectName("comboBox")self.comboBox.addItem("")self.comboBox.addItem("")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 520, 23))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)self.menubar.hovered['QAction*'].connect(self.menubar.close) # type: ignoreQtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.addModuleButton.setText(_translate("MainWindow", "添加文件"))self.label.setText(_translate("MainWindow", "Module.h:"))self.label_2.setText(_translate("MainWindow", "log信息:"))self.addModule_HwButton.setText(_translate("MainWindow", "添加文件"))self.label_3.setText(_translate("MainWindow", "Module_Hw.h:"))self.label_4.setText(_translate("MainWindow", "目标文件:"))self.label_5.setText(_translate("MainWindow", "备注:详细设计需要额外增加Module_Hw.h文件。"))self.StartGen.setText(_translate("MainWindow", "开始生成"))self.label_6.setText(_translate("MainWindow", "输出文件:"))self.comboBox.setItemText(0, _translate("MainWindow", "用户手册"))self.comboBox.setItemText(1, _translate("MainWindow", "详细设计"))
1.3. 页面效果
2. 参考资料
2.1. PyQt5 参考资料
1、安装教程:https://blog.csdn.net/qq_32892383/article/details/108867482
2、使用教程:https://zhuanlan.zhihu.com/p/269273821
2.2. tkinter 参考资料
1、https://blog.csdn.net/bigzql/article/details/116036439
2、https://www.runoob.com/python/python-gui-tkinter.html
3. 安装注意事项
3.1. 下载
PyqQt5和PyQt5-tools建议使用国内的镜像源,否则速度很慢
相关命令如下:
pip install -i https://mirrors.aliyun.com/pypi/simple/ Pyqt5-tools
pip install -i https://mirrors.aliyun.com/pypi/simple/ Pyqt5
注意:这里使用的是国内阿里云的镜像,网上查到的地址是http打头的,但实际运行时需要使用https的头才能正确下载,原因未知
3.2 Pycharm添加External Tools
教程中PyQt5的版本可能比较老,我目前(2024年03月)装的是5.15.10版本的PyQt5,designer.exe文件已经搬到…Python310\Lib\site-packages\qt5_applications\Qt\bin路径下面
安装教程截图
实际路径