In this article, we will learn about python pyqt. Let’s start!!!
What is PyQT?
One of the most used Python wrappers for the cross-platform C++ framework Qt is PyQt. Riverbank Computing Limited is the creator of PyQt. As a component of the Qt Project, Qt itself is being developed. For versions 4 and 5, PyQt offers bindings. A commercial license or the GPL version 3 are the two licensing options available for PyQt.
PyQt is a toolkit for GUI widgets. A Python interface is available for Qt, one of the best and most widely used cross-platform GUI libraries. PyQt was developed by RiverBank Computing Ltd. The most recent version of PyQt is available from riverbankcomputing.com, the program’s official website.
The PyQt API is a collection of modules that includes numerous classes and functions. The QtGui module provides all the graphical features, but the QtCore module only contains non-GUI functionality for working with files and directories, etc. There are also modules for working with XML (QtXml), SVG (QtSvg), SQL (QtSql), and other file types. Let’s learn more about PyQT in this TechVidvan article.
Versions of PyQT
There are two versions of PyQt: PyQt4 and PyQt5, both of which support building against Qt 4.x and 5.x respectively. For Python 2 and 3, both editions can be constructed. More than 620 classes in PyQt cover graphical user interfaces, XML handling, network connection, SQL databases, web browsing, and other Qt-compatible technologies.
PyQt is currently at version 5.11.3. It supports Qt 5.11.2 entirely.
Windows, Linux, Mac OS X, and different UNIX platforms all support PyQt4. Both iOS and Android support PyQt5.
PyQT Supporting Environment
A virtual environment is the best way to manage dependencies in Python. A local directory with the libraries necessary for a certain project makes up a virtual environment. On the other hand, a system-wide installation of those libraries would have an impact on all of your projects.
All of the widely used operating systems, including Windows, Linux, and Mac OS, are compatible with PyQt. It has both a GPL and a commercial license, making it dual-licensed.
PyQT for Windows
From the aforementioned download URL, you can download and set up the proper installer for the hardware architecture and Python version (2.7 or 3.4). (32-bit or 64-bit). Keep in mind that PyQt is available in two different versions: 4.8 and 5.5.
PyQT for Linux
Use the following command to install PyQt on Ubuntu or any other Debian Linux distribution:
sudo apt-get install python-qt4 or sudo apt-get install pyqt5-dev-tools
PyQT for Mac OS
PyQt for Mac binaries are hosted by the PyQtX project (http://sourceforge.net/projects/pyqtx/). Use the Homebrew installer by issuing the command below.
brew install pyqt
Creating a PyQt Application
The instructions below describe how to use PyQt to create a basic GUI application:
- Import QtGui module.
- Create an application object.
- A QWidget object creates a top-level window. Add a QLabel object to it.
- Set the caption of the label as “Hi World”.
- Define the size and position of the window by setGeometry() method.
- Enter the main loop of the application by app.exec_() method.
import sys
from PyQt4 import QtGui
def window():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
b = QtGui.QLabel(w)
b.setText("Hi World")
w.setGeometry(100,100,200,50)
b.move(50,20)
w.setWindowTitle(“PyQt”)
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
Major Classes in PyQt API
There are numerous classes and methods in the PyQt API. More than 20 modules define these classes. Here are a few of the modules that are used frequently:
- QtCore – the fundamental non-GUI classes that other modules use
- QtGui – components of a graphical user interface
- QtMultimedia – Low-level multimedia programming courses
- QtNetwork – Network programming courses
- QtOpenGL – classes that enable OpenGL
- QtScript – Classes for Qt Script evaluation
- QtSql – classes for integrating databases with SQL
- QtSvg – classes for showing SVG files’ contents
- QtWebKit – HTML rendering and editing classes
- QtXml – handling classes XML
- QtAssistant – assistance with internet help
- QtDesigner – expanding Qt Designer using classes
There are more than 400 classes in the PyQt API. At the top of the class, the hierarchy is the QObject class. It serves as the foundation for all Qt objects. In addition, the basic class for all paintable objects is QPaintDevice.
A GUI application’s primary settings and control flow are managed by the QApplication class. It has a main event loop where events produced by window elements and other sources are processed and sent to their destinations. Additionally, it manages system- and application-wide settings.
The base class for all user interface objects is QWidget, which is developed from QObject and QPaintDevice classes. The QWidget class also gives rise to the QDialog and QFrame classes. They have their system of subclasses.
Using Qt Designer
A GUI building program named Qt Designer is included with the PyQt installer. Without having to write any code, a GUI interface may be quickly developed using its straightforward drag-and-drop interface. However, it is not an IDE like Visual Studio. As a result, Qt Designer cannot build and debug the program.
Signals & Slots
A GUI-based application is event-driven, as opposed to console-style applications, which run sequentially. Events, such as clicking a button, choosing an item from a collection, or using the mouse, are what cause functions or methods to be executed.
Such events originate from the widgets that were utilized to construct the GUI interface. Every PyQt widget, which is a subclass of QObject, is made to emit a “signal” in response to one or more events. The signal doesn’t do anything on its own. It is “attached” to a “slot” instead. Any Python function that may be called can be the slot.
Qt’s signals feature enables you to respond to events like a button click from the user. The example that follows shows this. It has a button that, when pressed, displays the following message box:
from PyQt5.QtWidgets import *
app = QApplication([])
button = QPushButton('Click')
def on_button_clicked():
alert = QMessageBox()
alert.setText('You clicked the button!')
alert.exec()
button.clicked.connect(on_button_clicked)
button.show()
app.exec()
PyQt Layout Management
Using absolute coordinates in pixels, a GUI widget can be positioned inside the container window. The coordinates are positioned about the window’s dimensions as specified by the setGeometry() function.
A PushButton widget is added to the window and positioned 50 pixels to the right of the top left corner and 20 pixels below it.
However, this Absolute Positioning is not appropriate for the following reasons:
- Even if the window is shrunk or expanded, the widget’s location remains unchanged.
- On different display devices with varied resolutions, the appearance might not be consistent.
- It is challenging to change the layout because it can necessitate redesigning the entire form.
For more elegant handling of widget positioning inside the container, the PyQt API offers layout classes. Layout managers have the following benefits over absolute positioning:
- The window’s widgets are automatically resized.
- guarantees a consistent appearance on displays with various resolutions.
- Without redesigning, widgets can be dynamically added or removed.
QDialog Class
A QDialog widget displays a top-level window that is primarily used to solicit user input. It can be set up to be Modeless or Modal (where it blocks its parent window) (the dialogue window can be bypassed).
InputDialog, FileDialog, FontDialog, and other preconfigured Dialog widgets are available in PyQt API.
QMessageBox
QMessageBox is a frequently used modal dialogue that shows an informative message and, if desired, prompts the user to respond by clicking one of the on-screen buttons. Each standard button has a predefined role, a predefined hexadecimal number, and a caption.
Drag & Drop in PyQt
Drag and drop functionality is incredibly user-friendly. The user can copy or transfer things from one window to another in many desktop programs.
QDrag class is the foundation of MIME-based drag-and-drop data transmission. Data are connected to their relevant MIME types using QMimeData objects. It gets copied to the clipboard before being dropped via drag and drop.
Drag and drop functionality is supported by many QWidget objects. Those that permit data dragging have a setDragEnabled() method that needs to be set to true. The widgets, on the other hand, must react to drag-and-drop events to store the data that is dragged into them.
- When a dragging action enters the target widget, DragEnterEvent produces an event that is communicated to that widget.
- When the drag-and-drop operation is in process, DragMoveEvent is used.
- As the drag-and-drop operation exits the widget, a DragLeaveEvent is triggered.
- The completion of the drop triggers the DropEvent, on the other hand. The suggested action of the event may be conditionally accepted or refused.
Compile your app – Freezing PyQt programs
You now possess the fundamental information necessary to develop a GUI that responds to user input. Let’s say you created an app. On your PC, it operates. How can you pass it on to others so they can operate it too?
You may request that app users install Python and PyQt, as we did above, before distributing your source code. But it’s incredibly tedious (and usually impractical). We would like a standalone edition of your app. In other words, a binary executable that users may execute directly from their computers without the need to install anything.
Freezing is the term used in the Python community to describe the conversion of source code into a self-contained executable. Even though this problem is addressed by a large number of libraries, like PyInstaller, py2exe, cx Freeze, bbfreze, py2app, etc., freezing PyQt programs has historically been an incredibly challenging issue.
Conclusion
This TechVidvan tutorial dives right into PyQt, Python’s wrapper for the well-known Qt library. We’ve looked at some of the library’s core ideas, dived right into using it with Layout Managers, familiarised ourselves with Widgets, and produced several quite basic sample applications that teach you how to use them.

