因为工作中需要给交付团队开发桌面工具,考虑到交付团队多使用Mac,调研了一下发现PyQt5可以实现跨平台,满足工具开发需要,就用其开发了桌面工具。现以ubuntu开发环境为例总结一下开发过程。
目录
1.安装python3.7
2.安装PyQt5版本5.15.2
3.protobuf库使用
4.编写pyqt5程序
4.1设计界面并产生py代码
4.2封装mainwindow.py成MainWidget.py
4.3调用MainWidget.py
5.打包
i. 安装pyinstaller
ii. 打包
sudo apt-get install python3.7
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyqt5==5.15.2
另外一种安装方法:
sudo pip3 install PyQt5==5.15.2
i. 查看版本
sudo pip3 index versions protobuf
ii. 卸载
sudo pip3 uninstall protobuf
iii. 安装指定版本
sudo pip3 install protobuf==3.19.0
i. 安装Qt Designer并使用其设计界面
例如得到界面文件mainwindow.ui
ii. .ui文件转.py文件
python3 -m PyQt5.uic.pyuic -o mainwindow.py mainwindow.ui
mainwindow.ui
MainWindow
0
0
1071
709
MainWindow
-
-
-
Qt::Vertical
QSizePolicy::Fixed
20
78
-
-
OK
-
Cancel
mainwindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1071, 709)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.widget = QtWidgets.QWidget(self.centralWidget)
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.textEdit = QtWidgets.QTextEdit(self.widget)
self.textEdit.setObjectName("textEdit")
self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
self.verticalLayout.addWidget(self.widget)
spacerItem = QtWidgets.QSpacerItem(20, 78, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
self.verticalLayout.addItem(spacerItem)
self.widget_2 = QtWidgets.QWidget(self.centralWidget)
self.widget_2.setObjectName("widget_2")
self.gridLayout_2 = QtWidgets.QGridLayout(self.widget_2)
self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
self.gridLayout_2.setSpacing(6)
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButtonOk = QtWidgets.QPushButton(self.widget_2)
self.pushButtonOk.setObjectName("pushButtonOk")
self.gridLayout_2.addWidget(self.pushButtonOk, 0, 0, 1, 1)
self.pushButtonCancel = QtWidgets.QPushButton(self.widget_2)
self.pushButtonCancel.setObjectName("pushButtonCancel")
self.gridLayout_2.addWidget(self.pushButtonCancel, 0, 1, 1, 1)
self.verticalLayout.addWidget(self.widget_2)
MainWindow.setCentralWidget(self.centralWidget)
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.pushButtonOk.setText(_translate("MainWindow", "OK"))
self.pushButtonCancel.setText(_translate("MainWindow", "Cancel"))
MainWidget.py
"""
This is the MainWidget
"""
#coding=utf-8
import os
from mainwindow import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDesktopWidget, QPushButton
import threading, time
from datetime import datetime
from PyQt5.QtGui import QColor
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, pyqtSignal
import sys
from google.protobuf import json_format
class MainWidget(QtWidgets.QMainWindow):
signal_append_text = pyqtSignal(str)
"""
This is the MainWidget
"""
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui_init()
self.version = "1.0.0.10"
self.signal_append_text.connect(self.append_text)
def ui_init(self):
self.ui.pushButtonOk.clicked.connect(self.on_button_ok)
self.ui.pushButtonCancel.clicked.connect(self.on_button_cancel)
def emit_append_text_signal(self, text):
"""
发射追加文本信号
"""
self.signal_append_text.emit(text)
def append_text(self, text):
"""
edit上追加显示文本
"""
self.ui.textEdit.append(text)
def on_button_ok(self):
"""
on_button_ok
"""
self.emit_append_text_signal("click OK")
file_path = QFileDialog.getOpenFileName(self, "选择Test文件", ".", "*txt;;*bin")
self.ui.textEdit.append("select test files:")
self.ui.textEdit.append(str(file_path[0]))
def on_button_cancel(self):
"""
on_button_cancel
"""
self.emit_append_text_signal("click Cancel")
项目目录结构如下:
main.py
"""
This is the main module.
"""
#coding=utf-8
from PyQt5 import QtCore, QtGui, QtWidgets
from MainWidget import MainWidget
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_widget = MainWidget()#QtWidgets.QMainWindow()
main_widget.setWindowTitle("version: " + "1.0.0.1")
main_widget.show()
deskRect = QtWidgets.QApplication.desktop().screenGeometry()
x = deskRect.width() / 12
y = deskRect.height() / 12
main_widget.move(x, y)
main_widget.resize(deskRect.right() - 2 * x, deskRect.bottom() - 2 * y)
sys.exit(app.exec_())
运行结果如下:
python3 main.py
sudo pip3 install pyinstaller
pyinstaller -i tool.ico -w main.py
tar -zcvf anptools_v1.0.0.10.tar.gz dist
dist/main目录下的main即可运行