Pyqt5 Projects

PyQt5 Tutorial with Examples: Design GUI using PyQt in Python

It is a Python binding for the open-source widget-toolkit Qt, which also serves as a cross-platform application development framework. PyQt is a python binding for the Qt widget-toolkit. Qt is a well-known C++ framework for developing graphical user interfaces (GUIs) for all major desktop, mobile, and embedded platforms (supports Linux, Windows, MacOS, Android, iOS, Raspberry Pi, and more).

PyQt is a free software project produced and managed by Riverbank Computing, a company based in England, whereas Qt is a proprietary software project developed and maintained by The Qt Company, a Finnish company.

Features of PyQT

Find more about PyQt, which consists of more than six hundred classes that span a wide range of features such as animation.

Graphical User Interfaces (GUIs) are a type of user interface that is designed to look like a picture.
SQL Databases are a type of relational database.
Web-based toolkits
Processing of XML
Networking
These elements can be combined to create sophisticated user interfaces as well as stand-alone programmes. Qt is used by a large number of prominent corporations across many industries. LG, Mercedes, AMD, Panasonic, Harman, and other companies are examples.

PyQt Versions

Versions of the PyQt programming language
PyQt is available in two editions: PyQt4 and PyQt5. PyQt4 is the most recent version of the programme. Unlike PyQt5, which provides a binding for both the Qt 4.x and 5.x versions of the framework, PyQt4 provides just a binding for the 5.x versions of the Qt framework. This has resulted in PyQt5 being incompatibly incompatible with the deprecated modules of the previous version. In this Qt GUI lesson, PyQt5 will be used to demonstrate the examples that will be presented. Aside from these two variations, there are other others.

Riverbank Computing also offers PyQt3D, which is a set of Python bindings for the Qt3D 3D graphics technology. Qt3D is an application framework that may be used to develop real-time simulation systems that can render in 2D and 3D.

How to install PyQt5

PyQt5 installation can be accomplished in two methods, as demonstrated in this tutorial:

Making use of Wheel files
Constructing and installing a Qt (pronounced cute) application from source code is a complicated process, and the PyQt codebase contains built C++ and Python code underneath the surface. The effect is that building and installing it from the source code is more difficult than building and installing other python libraries. PyQt5 can, on the other hand, be installed quickly and easily utilising wheels.

Wheels are used for installation.
A new standard Python packaging and distribution format, known as Wheels, has been introduced. Simply put, a wheel is a ZIP archive that has been given a unique name and the.whl file extension to distinguish it from the rest. Installation of Wheels is made possible through the use of pip (Python’s package management), which is included by default in recent versions of the Python programming language.

As a result, if you have Python 3.4 or later installed, pip is already available to you. If you are using an older version of Python, you will need to download and install pip first before proceeding. You can look for instructions on how to do so at the following URL: https://pypi.org/project/pip/.

In order to install PyQt5,

The first step is to open the Command Prompt.
To begin, open the Command Prompt or PowerShell on your Windows computer.

Instructions on how to install PyQt5 (Step 2). Fill in the blanks with the information below.

pip install PyQt5 (Step 3) The installation was completed.
This step in this PyQt5 tutorial will download and install the PyQt5 whl package (which is around 50 MB in size) on your system.

PyQt5 should be installed.
A Windows binary for the version of Python that is currently installed on your computer can also be obtained as a backup option.

Proceed to the next step of this PyQt5 tutorial to develop your first graphical user interface (GUI) application after it is complete.

PyQt Concepts and Programs from the Ground Up
You are now prepared to build Python GUI design applications now that you have successfully installed PyQt5 on your PC.

For the purposes of this PyQt5 tutorial, let’s start with a straightforward app that simply displays an empty window on your screen.

In the IDLE window of your Python interpreter, type in the following code:

Basic PyQt Concepts and Programs

sys is imported from PyQt5.

The following code is generated by QtWidgets if name == ”
app is an abbreviation for QApplication (sys.argv)
QWidget is represented by the letter w. ()
w.resize(300,300)
“Guru99”) w.setWindowTitle(“Guru99”) w.display ()
sys.exit(app.exec_())
Save it as app.py (the name doesn’t matter) and press F5 to execute the programme on your computer. Alternatively, you can activate the application by double-clicking the saved file. If everything has been completed correctly, a new window with the title Guru99 will appear, as shown in the screenshot below.

PyQt5 Programming Guide
Great! It appears to be functioning. It’s not much, but it’s enough to get a grasp on the fundamentals. Let’s take a closer look at each of the lines in your programme in this PyQt lesson to see what they are doing.

derived from PyQt5.

QtWidgets is being imported. QApplication and QWidget are two examples of QWidget.
This statement imports into the current namespace all of the modules that are required to develop a graphical user interface (GUI). Throughout this Python Qt tutorial, you will be utilising the QtWidgets module, which contains all of the primary widgets.

app is an abbreviation for QApplication (sys.argv)
You are constructing an instance of the QApplication class in this case. As part of PyQt5, every UI application must generate an instance of the QApplication class, which serves as a type of entry point within the application. You will see errors if you do not make it yourself.

A list of command-line options that you can supply to an application when launching it through the shell or while automating the interface is stored in the system variable sys.argv.

The QApplications class in this PyQt5 example did not receive any arguments from you. So you don’t even have to worry about importing the system module if you replace it with the code in this section.

app is an abbreviation for QApplication ([])
QWidget is represented by the letter w. ()
Following that, we create an instance of the QWidget class. Almost everything you see in an app is a widget, as QWidget is the root class for all UI elements in the Qt programming language. This contains dialogue boxes, text, buttons, bars, and other similar elements. The ability to nest widgets, which means that you can have a widget inside another widget, which is inside yet another widget, is what allows you to construct complicated user interfaces. You will see an example of this in action in the following section.

Components and Widgets

w.resize(300,300)
Using the resize method of the QWidget class, you can change the dimensions of any widget. If you look at the example above, you have resized the window to be 300px by 300px.

It’s important to know that widgets can be layered within each other, and that the outermost widget (i.e., the widget with no parent) is referred to as a Window.

w.setWindowTitle(“Guru99”)
A string can be passed as an argument to the setWindowTitle() method, and the title of the window will be changed as a result of the string being passed. Guru99 will be displayed in the title bar of the PyQt5 demonstration.

w.show() show() does nothing more than display the widget on the computer screen.

sys.exit(app.exec_())
The app.exec_() method is responsible for starting the Qt/C++ event loop. As you are aware, PyQt is written mostly in C++ and makes extensive use of the event loop technology to support parallel execution. app.exec_() transfers control to Qt, which will only terminate the application if the user exits it from the graphical user interface (GUI). As a result, pressing the ctrl key will not terminate the application as it does in other Python programmes. Because Qt has control over the application, python events are not processed unless they are explicitly enabled within the programme. In addition, keep in mind that the exec function includes an underscore in its name; this is because exec() was previously used as a keyword in Python and the underscore resolves the naming issue.

Beyond the emptiness of the windows
We looked at how to create a simple widget in Qt in the previous portion of this tutorial. Creating more sophisticated interfaces with which people can actually interact is now a high-priority task. Start your IDLE programme once more and type the following.

sys is imported from PyQt5.

QtWidgets imports QApplication, QWidget, QLabel, QPushButton, QMessageBox, and QMessageBoxElements from Qt.

dialogue() is defined as follows: mbox = QMessageBox ()

mbox.setText is a method that sets the text of a box (“Your allegiance has been noted”)
mbox.setDetailedText returns the detailed text of the mbox (“You are now a disciple and subject of the all-knowing Guru”)
mbox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) sets the standard buttons for the QMessageBox.

Beyond empty windows

QLabel is an abbreviation for label (w)
setText (label) (“Behold the Guru, Guru99”)
label.move(100,130)
label.show()

btn = QPushButton (button = QPushButton) (w)
btn.setText(‘Beheld’)
btn.move(110,150)
btn.show() btn.clicked are two methods for displaying buttons.
connect(dialog)

System-wide exit (w.show() sys.exit(app.exec_())
Save the file as appone.py or whatever name you like, and then click F5 to execute the application. The IDLE will open a new window containing some text and a button, like shown below, if you haven’t committed any mistakes.

PyQt5 Programming Guide
As soon as you click on the button in the first window, a new message box will appear, containing the content that you had previously put in that window.
You may now toggle the visibility of additional text by clicking on the Hide Details/Show Details button on the toolbar.
As you can see, because we did not specify a window title in the message box, python generated one for us based on the information we provided.