Introduction
We have previously introduced the use of tkinter
creating graphical user interface (GUI) applications. Now we will introduce another artifact for creating GUI - pyQt5
, which can be used to create cross-platform desktop applications. PyQt5 implements a Python module set with 620 classes, 6000 functions and methods. PyQt5 has the following advantages: simple and easy to use, powerful functions, cross-platform support, complete documentation, high stability, ecological support, open source and free. In this article, we will provide a PyQt5 installation and configuration guide to ensure that we can start developing GUI applications smoothly.
Install PyQt5
We can install PyQt5 directly using the pip command, the command is as follows:
pip install pyqt5
After the installation is completed, we can start writing our program and check whether our installation is successful. The script is as follows:
from PyQt5 import QtWidgets
import sys
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv) # 初始化界面
MainWindow = QtWidgets.QWidget() # 生成一个主窗口
MainWindow.show() # 显示主窗口
sys.exit(app.exec_()) # 在主线程中退出
Run the script and the following interface will appear:
When the following page appears, it means that our installation is successful.
Install QtTools
Although PyQt5
after installation, we can start writing GUI programs, debugging and modification will be more cumbersome. We can install QtTools to facilitate us to modify our programs and facilitate our work.
Qt Tools contains two important tools:
- Qt Designer: graphical interface design tool, used to design graphical interfaces, generate .ui files, and store the properties of interfaces and controls in xml format
- PyUic: UI file conversion tool for parsing .ui files into .py files
The installation command is as follows:
pip install pyqt5-tools
Configure QtDesigner and PyUIC
Basically all Pythoners use pycharm to develop Python projects. We can also configure the environment of QtDesigner
and in pycharm PyUIC
and integrate them into PyCharm. Now let's introduce the configuration steps:
- Add Create Tools in PyCharm
- run
PyCharm
; - Select: from the top menu bar
File -> Settings
to pop upSeetings
the window; - Select: from the left menu bar
Tools -> ExternalTools
, click "+" on the right to pop upCreateTool
the window;
- Add QtDesigner tool
Fill in the CreateTool window in order:
- Name: Fill in "QtDesigner"
- Program: fill
designer.exe
in the path, as I filled in hereC:\xxxxx\xxxxPycharmProjects\qt5_project\venv\Lib\site-packages\qt5_applications\Qt\bin\designer.exe
Note: Fill in the path we installed designer.exe
using the command before .pip
pyqt5-tools
-
Arguments: can be left blank
-
Working directory: The saving path of the generated UI file. If you want to save the .ui file in the path of the current Project, fill it in; if
“$ProjectFileDir$”
you want to save the .ui file in a subdirectory of the current Project path\program
, fill it in“$ProjectFileDir$\program”
.
Fill in the picture below:
Click OK to complete the addition.
- Add PyUIC tools
Fill in the following in the CreateTool window:
- Name: Fill in "PyUIC"
- Program: Fill in the path of python.exe, for example:
D:\xxxxx\xxxxxx\PycharmProjects\qt5_project\venv\Scripts\python.exe
Filled in here is the Python interpreter path of my project
- Arguments: fill in
"-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py”
- Working directory: Fill in the saving path to convert the .ui file into a .py file. If you want to save the .py file in the path of the current Project, fill it in; if you want to
“$ProjectFileDir$”
save the .py file in a subdirectory of the current Project path\program
, then Fill in“$ProjectFileDir$\program”
.
After filling out the CreateTool window, click "OK" to complete the addition of the QtDesigner tool.
Summarize
This article mainly introduces the installation and configuration of PyQt5. Later, we will introduce in detail the development of GUI using PyQt5.