最近几天新入坑了python的GUI设计,回想一下我为什么会入门这个???好像是在知乎上看到 你都用 Python 来做什么? 这篇文章,看到有人回答说将python打包成exe文件。

PyQt5简介

PyQt5是作为一套Python模块实现的。它有超过620个的类和6000个函数和方法。它是一个跨平台的工具集,可以运行在所有的主流操作系统上,包括Unix, Windows和Mac OS。
PyQt5中的类可划分为以下几个模块,包括:

  • QtCore模块包含了核心非GUI功能.这个模块是用于时间、文件和目录、变量数量类型、数据流、URLs、mime(多用途因特网邮件扩展)类型、线程和进程。
  • QtGui模块包含了窗口系统集成、事件处理、2D绘画、基本成像、字体和文本.
  • QtWidgets模块包含了一系列UI元素,用于创建典型的桌面风格用户接口.
  • QtMultimedia包含了处理多媒体内容和访问摄像机、无线电等功能的APIs.
  • QtBluetooth模块包含了设备扫描和设备接接与互动.
  • QtNetwork模块包含了网络编程的类.这些类用于TCP/IP和UDP客户端、服务端编程,使之网络编程更加方便和快捷.
  • QtPositioning模块包含了使用一系列包括卫星、Wi-Fi或文本文件在内的可能源变量来决定位置的类.
  • Enginio模块包含了用于访问Qt云服务管理应用运行时的客户端的库.
  • QtWebSockets模块包含了实现WebSocket协议的类.
  • QtWebKit包含了基于WebKit2库的实现网络浏览器的类.
  • QtWebKitWidgets包含了基于WebKit1库的实现网络浏览器的类,它是用在基于应用的QtWidgets上的.
  • QtXml包含了用于XML文件的类.这个模块提供了SAX和DOM接口的实现.
  • QtSvg模块提供了用于显示SVG文件内容的类.SVG(Scalable Vector Graphics)是用于描述在XML中二维图像和图像应用的语言.
  • QtSql模块提供了用于数据库的类.
  • QtTest包含了PyQt5的单元测试功能.
  • PyQt5安装及Qt Designer介绍

    pyqt5安装

    使用Anaconda,其实Anaconda里面是含有pyqt的,在环境里搜索可以看到,但是针对实际开发,并没有全部的qt5工具,所以需要再次重新安装PyQt。这里选择PyQt5,通过创建 conda环境 ,然后在环境中使用如下命令进行PyQt5的安装。

    1
    pip install PyQt5

    安装常用的Qt工具

    使用如下命令进行安装,工具中包含有 Qt designer

    1
    pip install PyQt5-tools

    在PyQt中编写UI界面可以直接通过 代码 来实现,也可以通过 Qt Designer 来完成。Qt Designer的设计符合MVC的架构,其实现了视图和逻辑的分离,从而实现了开发的便捷。

    Qt Designer中的操作方式十分灵活,其通过拖拽的方式放置控件可以随时查看控件效果。

  • Qt Designer生成的.ui文件(实质上是XML格式的文件)也可以通过pyuic5工具转换成.py文件。
  • Qt Designer随PyQt5-tools包一起安装,其安装路径在“Python安装路径\Lib\site-packages\pyqt5-tools”下。
  • 关于安装 新版PyQt5和PyQt5_tools 后配置QtDesigner是 找不到designer.exe 的问题。按照网上的操作方法,装完PyQt5和PyQt5_tools后,配置QtDesigner时找不到designer.exe;按网上的路径查找的话根本就找不到;其实新版本安装的路径不是网上说的那种,已经换地方了。例如我的 designer.exe路径 是:D:\Program Files\Miniconda3\envs\pyqt05\Lib\site-packages\qt5_applications\Qt\bin\designer.exe。
  • 若要启动Qt Designer可以直接到上述目录下,双击designer.exe打开Qt Designer;或 将上述路径加入环境变量 ,在命令行输入designer打开;或在PyCharm中将其配置为外部工具打开。
  • 下面以PyCharm为例,讲述PyCharm中Qt Designer的配置方法。

    配置Pycharm

    推荐使用在PyCharm中将其配置为外部工具打开,主要包括添加以下三个外部工具。

    * .exe Qt Desinger *./designer.exe 打开Qt Designer界面,对软件的界面进行设计 PyUIC *./python.exe 将Qt Designer设计的UI文件转换为.py文件 PyRCC *./pyrcc5.exe 将资源文件(如图片等)转换成python代码能识别的文件 Program designer.exe路径,一般在anaconda配置环境中的pyqt5_tools中,如:D:\Program Files\Miniconda3\envs\pyqt05\Lib\site-packages\pyqt5_tools\designer.exe Arguments $FileDir$\$FileName$或者为空(不设置) Working dierctory $FileDir$
    添加PyUIC
    Program python.exe路径,一般在anaconda配置的环境中,如:D:\Program Files\Miniconda3\envs\pyqt05\python.exe Arguments -m PyQt5.uic.pyuic $FileName​$ -o $FileNameWithoutExtension$.py Working dierctory $FileDir$
    添加PyRCC
    Program pyrcc5.exe路径,一般在anaconda配置的环境中的Scripts中,如:D:\Program Files\Miniconda3\envs\pyqt05\Scripts\pyrcc5.exe Arguments $FileName​$ -o $FileNameWithoutExtension$_rc.py Working dierctory $FileDir$

    Qt Designer界面简介

    刚打开的Qt Designer,会弹出如下图所示的窗口。

    要想创建新的Forms,给出了5个模板,其中Widget与Main Window最为常用。这里我们选择创建一个Main Window。

    上面界面的最左侧菜单为Widget Box,且Widget Box中包含PyQt5中的所有Widget组件,我们可以从左侧的Widget Box中拖拽出诸如Button、View和Input等组件到中间的窗口(我们刚刚创建的Main Window)中。点击Form -> Preview(快捷键为 Ctrl+R )则可以预览我们设计好的界面,也可以用Preview In来选择在相应的主题风格下预览。

    接下来,我们试着拖拽一个Label与Button进入主窗口(Main Window)。

    此时在右上角的Object Inspector(对象查看器)中可以看到主窗口中的已放置的对象(label与pushButton)以及其相对应的Qt类。

    以Label为例,此时我们点击Main Window中的label或是在Object Inspector中选取label后,查看右侧的一块区域——Property Editor(属性编辑器)。

    其主要包含属性有如下:

    PS:将minimumSize和maximumSize设为一样的数值之后,则窗口的大小固定。

    最右下角的部分则为Resource Browser(资源浏览器),资源浏览器中可以添加相应地如图片素材,作为Label或Button等控件的背景图片等。

    Qt Designer的UI文件

    使用Qt Designer设计保存的文件为.ui格式的文件。
    通过保存并使用记事本等软件打开,我们可以看到.ui文件的内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralwidget">
    <widget class="QLabel" name="label">
    <property name="geometry">
    <rect>
    <x>240</x>
    <y>80</y>
    <width>72</width>
    <height>15</height>
    </rect>
    </property>
    <property name="text">
    <string>TextLabel</string>
    </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
    <property name="geometry">
    <rect>
    <x>240</x>
    <y>120</y>
    <width>93</width>
    <height>28</height>
    </rect>
    </property>
    <property name="text">
    <string>PushButton</string>
    </property>
    </widget>
    </widget>
    <widget class="QMenuBar" name="menubar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>26</height>
    </rect>
    </property>
    </widget>
    <widget class="QStatusBar" name="statusbar"/>
    </widget>
    <resources/>
    <connections/>
    </ui>

    从.ui文件的第一行我们便能看出,其实质是一个XML文件。ui文件中存放了在主窗口中的一切控件的相关属性。使用XML文件来存储UI文件,具有高可读性和移植性,因此我们可以方便地将.ui文件转换到.py文件,从而使得我们可以使用Python语言在设计的GUI上面编程。

    将.ui文件转换为.py文件

    pyuic可以将.ui文件转换为为.py文件,实现此过程的方法有三个:

    方法一: 使用Python命令

    1
    python -m PyQt5.uic.pyuic demo.ui -o demo.py

    方法二: 直接调用pyuic5命令

    1
    pyuic5 -o demo.py demo.ui

    方法三: 如果在PyCharm中设置了PyUIC扩展插件

    则直接在PyCharm中,找到.ui文件,右键 打开菜单找到External Tools->PyUIC。点击PyUIC之后,我们在相应工程目录下会产生一个.py文件。(注意,.ui文件必须存放在我们的External Tools中设置的相应项目目录下)

    转换完成之后,打开.py文件如下。观察这个文件,可以看到如果不通过Qt Designer来制作界面的话,我们将会一次次地调试程序,来讲按钮和Label等放在合适的位置,这将是极其痛苦的过程。而通过Qt Designer,我们可以快速地制作UI,并生成Python的代码,从而实现快速地UI的开发。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    # -*- coding: utf-8 -*-

    # Form implementation generated from reading ui file 'mainWindow.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.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(240, 80, 72, 15))
    self.label.setObjectName("label")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(240, 120, 93, 28))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
    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", "TextLabel"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))

    使用转换的.py文件进行界面显示

    然而,此时若运行这个转换好的Python文件是无法显示任何窗口的。因为这个Python文件只有定义主窗口以及其控件的代码,并没有程序入口的代码。为了秉持视图与逻辑分离的原则,我们再编写一个新的脚本来调用这个文件,并且创建一个窗口。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from mainWindow import *


    class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
    super(MyWindow, self).__init__(parent)
    self.setupUi(self)


    if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyWindow()
    myWin.show()
    sys.exit(app.exec_())

    通过上述代码,我们继承了QMainWindow和Ui_MainWindow类,使用构造方法构造主窗口,并定义了程序的入口,通过创建QApplication对象来创建Qt窗口。其运行结果如下:

    PyQT5:ImportError: DLL load failed: 找不到指定的模块

    可以选择重新安装PyQt5:

    1
    2
    pip uninstall PyQt5
    pip install PyQt5

    详细请看 (已解决)PyQT5:ImportError: DLL load failed: 找不到指定的模块 或者见参考博客3。

    参考博客:

    1. https://www.jianshu.com/p/5b063c5745d0

    2. https://blog.csdn.net/hon8215/article/details/110673015