当前位置:   article > 正文

Ubuntu下PyQt5使用总结_ubuntu pyqt5

ubuntu pyqt5

       因为工作中需要给交付团队开发桌面工具,考虑到交付团队多使用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. 打包


1.安装python3.7

sudo apt-get install python3.7

2.安装PyQt5版本5.15.2

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyqt5==5.15.2
另外一种安装方法:
sudo pip3 install PyQt5==5.15.2

3.protobuf库使用

i. 查看版本
sudo pip3 index versions protobuf
ii. 卸载
 sudo pip3 uninstall protobuf
iii. 安装指定版本
sudo pip3 install protobuf==3.19.0

4.编写pyqt5程序

4.1设计界面并产生py代码

i. 安装Qt Designer并使用其设计界面
例如得到界面文件mainwindow.ui


ii. .ui文件转.py文件
python3 -m PyQt5.uic.pyuic -o mainwindow.py mainwindow.ui

 4.2封装mainwindow.py成MainWidget.py

mainwindow.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>1071</width>
  10. <height>709</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <layout class="QVBoxLayout" name="verticalLayout">
  18. <item>
  19. <widget class="QWidget" name="widget" native="true">
  20. <layout class="QGridLayout" name="gridLayout">
  21. <item row="0" column="0">
  22. <widget class="QTextEdit" name="textEdit"/>
  23. </item>
  24. </layout>
  25. </widget>
  26. </item>
  27. <item>
  28. <spacer name="verticalSpacer">
  29. <property name="orientation">
  30. <enum>Qt::Vertical</enum>
  31. </property>
  32. <property name="sizeType">
  33. <enum>QSizePolicy::Fixed</enum>
  34. </property>
  35. <property name="sizeHint" stdset="0">
  36. <size>
  37. <width>20</width>
  38. <height>78</height>
  39. </size>
  40. </property>
  41. </spacer>
  42. </item>
  43. <item>
  44. <widget class="QWidget" name="widget_2" native="true">
  45. <layout class="QGridLayout" name="gridLayout_2">
  46. <item row="0" column="0">
  47. <widget class="QPushButton" name="pushButtonOk">
  48. <property name="text">
  49. <string>OK</string>
  50. </property>
  51. </widget>
  52. </item>
  53. <item row="0" column="1">
  54. <widget class="QPushButton" name="pushButtonCancel">
  55. <property name="text">
  56. <string>Cancel</string>
  57. </property>
  58. </widget>
  59. </item>
  60. </layout>
  61. </widget>
  62. </item>
  63. </layout>
  64. </widget>
  65. <widget class="QStatusBar" name="statusBar"/>
  66. </widget>
  67. <layoutdefault spacing="6" margin="11"/>
  68. <resources/>
  69. <connections/>
  70. </ui>

mainwindow.py

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'mainwindow.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.2
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. from PyQt5 import QtCore, QtGui, QtWidgets
  9. class Ui_MainWindow(object):
  10. def setupUi(self, MainWindow):
  11. MainWindow.setObjectName("MainWindow")
  12. MainWindow.resize(1071, 709)
  13. self.centralWidget = QtWidgets.QWidget(MainWindow)
  14. self.centralWidget.setObjectName("centralWidget")
  15. self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
  16. self.verticalLayout.setContentsMargins(11, 11, 11, 11)
  17. self.verticalLayout.setSpacing(6)
  18. self.verticalLayout.setObjectName("verticalLayout")
  19. self.widget = QtWidgets.QWidget(self.centralWidget)
  20. self.widget.setObjectName("widget")
  21. self.gridLayout = QtWidgets.QGridLayout(self.widget)
  22. self.gridLayout.setContentsMargins(11, 11, 11, 11)
  23. self.gridLayout.setSpacing(6)
  24. self.gridLayout.setObjectName("gridLayout")
  25. self.textEdit = QtWidgets.QTextEdit(self.widget)
  26. self.textEdit.setObjectName("textEdit")
  27. self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
  28. self.verticalLayout.addWidget(self.widget)
  29. spacerItem = QtWidgets.QSpacerItem(20, 78, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
  30. self.verticalLayout.addItem(spacerItem)
  31. self.widget_2 = QtWidgets.QWidget(self.centralWidget)
  32. self.widget_2.setObjectName("widget_2")
  33. self.gridLayout_2 = QtWidgets.QGridLayout(self.widget_2)
  34. self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
  35. self.gridLayout_2.setSpacing(6)
  36. self.gridLayout_2.setObjectName("gridLayout_2")
  37. self.pushButtonOk = QtWidgets.QPushButton(self.widget_2)
  38. self.pushButtonOk.setObjectName("pushButtonOk")
  39. self.gridLayout_2.addWidget(self.pushButtonOk, 0, 0, 1, 1)
  40. self.pushButtonCancel = QtWidgets.QPushButton(self.widget_2)
  41. self.pushButtonCancel.setObjectName("pushButtonCancel")
  42. self.gridLayout_2.addWidget(self.pushButtonCancel, 0, 1, 1, 1)
  43. self.verticalLayout.addWidget(self.widget_2)
  44. MainWindow.setCentralWidget(self.centralWidget)
  45. self.statusBar = QtWidgets.QStatusBar(MainWindow)
  46. self.statusBar.setObjectName("statusBar")
  47. MainWindow.setStatusBar(self.statusBar)
  48. self.retranslateUi(MainWindow)
  49. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  50. def retranslateUi(self, MainWindow):
  51. _translate = QtCore.QCoreApplication.translate
  52. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  53. self.pushButtonOk.setText(_translate("MainWindow", "OK"))
  54. self.pushButtonCancel.setText(_translate("MainWindow", "Cancel"))

MainWidget.py

  1. """
  2. This is the MainWidget
  3. """
  4. #coding=utf-8
  5. import os
  6. from mainwindow import Ui_MainWindow
  7. from PyQt5 import QtCore, QtGui, QtWidgets
  8. from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDesktopWidget, QPushButton
  9. import threading, time
  10. from datetime import datetime
  11. from PyQt5.QtGui import QColor
  12. from PyQt5 import QtGui
  13. from PyQt5.QtCore import Qt, pyqtSignal
  14. import sys
  15. from google.protobuf import json_format
  16. class MainWidget(QtWidgets.QMainWindow):
  17. signal_append_text = pyqtSignal(str)
  18. """
  19. This is the MainWidget
  20. """
  21. def __init__(self):
  22. QtWidgets.QMainWindow.__init__(self)
  23. self.ui = Ui_MainWindow()
  24. self.ui.setupUi(self)
  25. self.ui_init()
  26. self.version = "1.0.0.10"
  27. self.signal_append_text.connect(self.append_text)
  28. def ui_init(self):
  29. self.ui.pushButtonOk.clicked.connect(self.on_button_ok)
  30. self.ui.pushButtonCancel.clicked.connect(self.on_button_cancel)
  31. def emit_append_text_signal(self, text):
  32. """
  33. 发射追加文本信号
  34. """
  35. self.signal_append_text.emit(text)
  36. def append_text(self, text):
  37. """
  38. edit上追加显示文本
  39. """
  40. self.ui.textEdit.append(text)
  41. def on_button_ok(self):
  42. """
  43. on_button_ok
  44. """
  45. self.emit_append_text_signal("click OK")
  46. file_path = QFileDialog.getOpenFileName(self, "选择Test文件", ".", "*txt;;*bin")
  47. self.ui.textEdit.append("select test files:")
  48. self.ui.textEdit.append(str(file_path[0]))
  49. def on_button_cancel(self):
  50. """
  51. on_button_cancel
  52. """
  53. self.emit_append_text_signal("click Cancel")


4.3调用MainWidget.py

项目目录结构如下:

main.py

  1. """
  2. This is the main module.
  3. """
  4. #coding=utf-8
  5. from PyQt5 import QtCore, QtGui, QtWidgets
  6. from MainWidget import MainWidget
  7. import sys
  8. if __name__ == "__main__":
  9. app = QtWidgets.QApplication(sys.argv)
  10. main_widget = MainWidget()#QtWidgets.QMainWindow()
  11. main_widget.setWindowTitle("version: " + "1.0.0.1")
  12. main_widget.show()
  13. deskRect = QtWidgets.QApplication.desktop().screenGeometry()
  14. x = deskRect.width() / 12
  15. y = deskRect.height() / 12
  16. main_widget.move(x, y)
  17. main_widget.resize(deskRect.right() - 2 * x, deskRect.bottom() - 2 * y)
  18. sys.exit(app.exec_())

运行结果如下:

python3 main.py

 


5.打包

 i. 安装pyinstaller


sudo pip3 install pyinstaller


ii. 打包

pyinstaller -i tool.ico -w main.py


tar -zcvf  anptools_v1.0.0.10.tar.gz dist

dist/main目录下的main即可运行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/138682
推荐阅读
相关标签
  

闽ICP备14008679号