Installing and Using PyQt5 in Python
Installing and Using PyQt5 in Python
One of the features of Python that makes it so powerful is the ability to take existing libraries, written
in C or C++, and make them available as Python extension modules. Such extension modules often
They are called bindings for the library.
Installation
Go to the page
[Link]
SIP is a collection of tools that makes it very easy to create Python bindings for C and C++ libraries.
SIP is provided [Link] install it, run the following command: pip install sip
sip
The main purpose of thesipThe module is to provide functionality common to all SIP generated bindings.
It is loaded automatically and most of the time you will completely ignore it. However, it does expose
some functionality that can be used by applications.
Wheels are installed using the pip program that is included with current versions of Python.
This will install the wheel for your platform and your version of Python (assuming both are supported).
The wheel will be automatically downloaded from PyPI.
If you get an error message saying that no downloads could be found that satisfy the requirement then
You are probably using an unsupported version of Python.
Make sure that the environment variables of your operating system are properly directed, according to where you have
Python and PyQt5 installed:
To ensure that the installation was successful, run the following Python code: In IDLE
import PyQt5
If no errors appeared, it means that PyQt5 was installed correctly, but if any error appears, it may be that
You are using an incompatible version of Python.
Prior to the release of SIP v5 the only way to build PyQt5 (and related projects) was based on
[Link]. This method is now deprecated and will be removed when SIP v6 is released
(which is expected to be mid-2020).
[Link] supports both SIP v4 and SIP v5. Therefore you can move to SIP v5 without needing to
change the way you build PyQt5 at the same time.
One way to create applications with graphical interfaces is by using PtQy5 Designer.
Installation:
PyQt5 Designer is included with the PyQt5 tools. To install it, you need to install the tools of
PyQt5.
After the installation, you can find the PyQt5 designer in this location:
C:\Users\jeama\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\pyqt5_tools\Qt\bin
Additionally, if Python was installed only for your current user, you can find PyQt5 designer in this location:
C:\Users\jeama\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\pyqt5_tools\Qt\bin
You can create a shortcut to this program instead of going to this location every time you want to run it.
Now we can make use of its libraries by importing them into the Python code. It corresponds to a large number of classes.
grouped in libraries or modules. Each of these classes represents the different graphical elements (windows,
buttons, etc.), that we can use, and whose handling we will carry out through a large number of predefined methods that
they come in them.
Open the file [Link] and you will see a dialog box that asks about the form template you want.
to use.
Dialog box with buttons at the bottom: create a form with the Accept and Cancel buttons at the bottom.
bottom right of the form.
Dialog box with buttons on the right: create a form with the Accept and Cancel buttons on the side
top right of the form.
Dialog box without buttons: create a blank form.
Main window: create a window with a menu bar and a toolbar, which is inherited from
QMainWindow.
Widget: create a widget that inherits from the QWidget class, unlike dialog templates that are inherited
from the QDialog class. (widgets are components)
QWidget is the base class used for all GUI elements in PyQt5.
QDialog is used to ask the user about something, such as asking them to accept or reject something, or maybe to request some
input, esta plantilla tiene como clase base QWidget.
QMainWindow is the largest template, here you can place the toolbar, menu bar, status bar
State and other widgets do not have a built-in margin for buttons like those in QDialog.
1. Creating applications with PyQt5 Designer: Load .ui or convert .ui to .py
Open PyQt5 designer, select the Main Window template and click the create button.
Then from the file menu, click on save as and give it the name example;
PyQt5 designer will export the form to an XML file with the .ui extension ([Link]).
To load the .ui file in the Python code, you can use the loadUI() function from uic like this:
Create a Python program called [Link], with the following code that calls the [Link] file created:
If the code is executed, a window appears with nothing more than a label. That means the ui file was loaded.
correctly.
We will use [Link]([Link]()) instead of using [Link]() directly to send the correct exit code.
tomain processor the calling process.
If you use [Link]() directly, the application will respond with zero, which means it was successful, and this will happen
even if the application fails.
There is a second way to convert the .ui file to a Python code file, Pyuic5 means version 5 of the
Python user interface converter.
From the command line of the operating system, execute the command $ pyuic5 [Link] -o [Link]
If you run this code, you should see the same window again as we did in the first method.
Note: You can use IDLE to create it and run it.
The advantage of using the second method is that all the widgets are imported, while the first method only
load the .ui file and you will need to know the names of widgets.
Another benefit of using the second method is speed, as it does not require performing an XML analysis to load the
user interface.
It can be said that converting the .ui file into a .py file is safer in coding and faster in loading.