0% found this document useful (0 votes)
14 views6 pages

Installing and Using PyQt5 in Python

This document describes how to install and use Qt in Python. It explains how to install the necessary libraries such as SIP and PyQt5 using pip, and how to build PyQt5. It also covers how to create applications with graphical interfaces using the PyQt5 designer, either by loading .ui files or converting them to .py files.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Installing and Using PyQt5 in Python

This document describes how to install and use Qt in Python. It explains how to install the necessary libraries such as SIP and PyQt5 using pip, and how to build PyQt5. It also covers how to create applications with graphical interfaces using the PyQt5 designer, either by loading .ui files or converting them to .py files.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Qt 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]

1.1 Download PyQt5

1.2 Before building PyQt5, it must be built and installedSIP

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.

1.3 Then install PyQt5

pip install PyQt5


Installing from Wheels
Wheels are the standard Python packaging format for pure Python or binary extension modules.
as PyQt5. Only Python v3.5 and later are supported. Wheels are provided for 32- and 64-bit Windows,
64-bit macOS and 64-bit Linux. These correspond with the platforms for which The Qt Company
provide binary installers.

Wheels are installed using the pip program that is included with current versions of Python.

Installing the GPL Version

To install the wheel for the GPL version of PyQt5, run:

pip install PyQt5

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.

1.4 Build PyQt5

Building PyQt5 with [Link]

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.

Then run python3 [Link]

2. Creating applications with PtQy5 Designer:

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.

pip3 install PyQt5-tools

Where is PyQt5 designer located?


PyQt5 designer runs with the file [Link]

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.

3. How to use PyQt5 designer

Open the file [Link] and you will see a dialog box that asks about the form template you want.
to use.

There are five templates available:

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, QDialog, and QMainWindow

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

Using PyQt5 designer:

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]).

Now, to use this design, there are two ways:

Load the .ui file into Python code.


Convert the .ui file into a .py file using pyuic5.

1.1 Load the .ui file into Python code

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:

from PyQt5 import QtWidgets, uic


import sys
app = [Link]([])
win = [Link]("[Link]") #especifique su archivo .ui
[Link]()
[Link]([Link]())

If the code is executed, a window appears with nothing more than a label. That means the ui file was loaded.
correctly.

Note: You can use IDLE to create and run it.

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.

1.2 Convert .ui files to a .py file using pyuic5

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]

This statement creates a new file named [Link].


Create a Python program called [Link], with the following code that calls the [Link] file created as a
library

from PyQt5 import QtWidgets


from example import Ui_MainWindow # import our generated file
import sys
class mywindow([Link]):
def __init__(self):
super(mywindow, self).__init__()
[Link] = Ui_MainWindow()
[Link](self)
app = [Link]([])
application = mywindow()
[Link]()
[Link]([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.

You might also like