Table of contents
Verify successful installation
This article mainly introduces how to install PyQt5 under the Window platform and how to develop and configure it under PyCharm.
Install
Install PyQt5
PyQt5
There are two installation methods, one is to download the source code from the official website and the other is to use pip
installation.
Here I recommend everyone to use pip
installation. Because it will automatically select the appropriate PyQt5
version based on your Python version, if you manually download the source code and install it, you will inevitably make a selection error. It is recommended to use a more secure installation method.
pip3 install PyQt5
In addition, if your network access to the external network is not very good, it is recommended to use Douban's mirror download, otherwise it will be very slow or the installation will fail.
pip install PyQt5 -i https://pypi.douban.com/simple
Install PyQt5-tools
PyQt5
Commonly used Qt tools are no longer provided, such as the graphical interface development tool Qt Designer and the international translation tool Liguist. If these are used in development, you must install the Qt tools yourself.
Here we still use script installation.
pip install PyQt5-tools
Or use the mirror download:
pip install PyQt5-tools -i https://pypi.douban.com/simple
Install QT Designer
In addition, window
the platform is PyQt-tools
installed by default QT Designer.exe
.
Verify successful installation
Execute the following code:
import sys from PyQt5.QtWidgets import QWidget, QApplication app = QApplication(sys.argv) widget = QWidget() widget.resize(640, 480) widget.setWindowTitle("Hello, PyQt5!") widget.show() sys.exit(app.exec())
If no error is reported and a window titled "Hello, PyQt5!" pops up, the installation is successful.
Developed using PyCharm
The installation PyCharm
will not be explained here. Just click "Next" each time and it will be installed by default.
What I installed here is the community version.
After the installation is successful, open PyCharm
Let's set up two extern tools
.
-
Open QT Designer through PyCharm
-
xxx.ui
Convert file toxxx.py
file via PyCharm
Let’s fully explain this part by creating a new project.
New Construction
For example, let's create a new helloword
project. Select File->New Project
New project name:helloworld
Environment managers such as Pipenv
, Virtualenv
, and are not used here .Conda
Python` version can be specified. Currently I am using `Python3.9
The interface after successful creation:
Add external tools
PyCharm
->File->Settings->Tools->External Tools
Then select "+"
Here is a screenshot after I added it successfully
Let’s add itQT Designer
first , you can refer to the screenshot settings.
-
"Name": This can be customized
-
"Program": The installation directory of "designer.exe" is selected. For details, please refer to the QT Designer section.
-
"Working directory": Configure according to the actual situation. Here I use macros directly.
$FileDir$
Add toPyuic
-
"Name": This can be customized
-
"Program": Select the installation directory of "pyuic5.exe"
-
"Arguments":
$FileName$ -o $FileNameWithoutExtension$.py
-
It means converting the selected
xxx.ui
file intoxxx.py
a file with the same name -
What needs special attention is that you need to right-click and select the corresponding file when executing
xxx.ui
, otherwise an error will occur.
-
-
"Working directory": Configure according to the actual situation. Here I use the macro $FileDir$ directly.
helloworld project
New new.ui
file
Click Create
The interface after successful creation
Add a Label
Enter helloworld
Make the font bigger
Save ui file
Here I namedhelloworld.ui
keep
Files PyCharm
can be seen inhelloworld.ui
Convert helloworld.ui
the file to helloworld.py file.
Again, be sure to select ui
the file you are using to convert.
Be sure to select the corresponding ui file
New helloworld.py
files can be seen
Run to see the final effect.
The interface after running
About QT
QT SDK
Includes Qt
libraries, libraries , Qt Creator IDE
and Qt-tools
libraries, all in one easy-to-install package.
Qt Creator
Users do not need a separate Qt Designer
program, but there are many Qt
users who do not use it Qt Creator
. At this time ui
, they can only use it to edit files Qt Designer
.