PyQt Tutorial: Python GUI Designer

What is PyQt?

PyQt is a python binding of the open-source widget-toolkit Qt, which also functions as a cross-platform application development framework. Qt is a popular C++ framework for writing GUI applications for all major desktop, mobile, and embedded platforms (supports Linux, Windows, MacOS, Android, iOS, Raspberry Pi, and more).

PyQt is developed and maintained by Riverbank Computing, a company based in England, whereas Qt is developed by a Finnish firm called The Qt Company.

In this PyQT tutorial, you will learn:

Features of PyQT

Here, are Important features of PyQt:

PyQt consists of more than six hundred classes covering a range of features such as

  • Graphical User Interfaces
  • SQL Databases
  • Web toolkits
  • XML processing
  • Networking

These features can be combined to create advanced UIs as well as standalone applications. A lot of major companies across all industries use Qt. Some examples are LG, Mercedes, AMD, Panasonic, Harman, etc.

PyQt Versions

PyQt is available in two editions, PyQt4 and PyQt5. PyQt4 provides glue code for binding 4.x and 5.x versions of the Qt framework while PyQt5 provides a binding for only the 5.x versions. As a result, PyQt5 is not backward compatible with the deprecated modules of the older version. In this tutorial, PyQt5 will be used for the demonstration of examples. Apart from these two versions,

Riverbank Computing also provides PyQt3D—the python bindings for the Qt3D framework. Qt3D is an application framework used to create real-time simulation systems with 2D/3D rendering.

How to install PyQt

There are two ways of installing PyQt.

  • Using Wheel files
  • Building and Installing from Source

Qt (pronounced cute) is a complex system, and the PyQt codebase contains compiled C++ and Python code under the hood. As a result, it is a complicated process to build and install it from the source compared to other python libraries. However, you can easily install PyQt5 using wheels.

Installation with wheels

Wheels are the new standard Python packaging and distribution format. Simply speaking, a wheel is a ZIP archive with a special name and .whl file extension. Wheels can be installed using pip (Python’s package manager), which is included by default in the recent versions of Python.

So, if you have Python 3.4 or later installed, you already have pip. If, however, you are using an older version of Python, you must download and install pip before going forward. You can search for instructions for that at this link: https://pypi.org/project/pip/.

To install PyQt5,

Step 1) Alternatively, you can also download a Windows binary for the version of python installed on your computer.

Once it is complete, proceed to the next section to write your first GUI app.

Basic PyQt Concepts and Programs

Now that you have successfully installed PyQt5 in your computer, you are ready to write GUI applications in python.

Let’s start with a simple app which will display an empty window on your screen.

Fire up your python IDLE and type in the following:

Beyond empty windows

In the previous section, you saw how to make a basic widget in Qt. It’s now time to make some more involved interfaces with which the users can truly interact. Again, fire up your IDLE and write the following.

  • Qt: It combines all the classes/modules mentioned below into a single module. It considerably increases the memory used by the application. However, it’s easier to manage the framework by only importing one module.
  • QtCore: Contains the core non-graphical classes used by other modules. This is where the Qt event loop, signals, and slot-connectivity, etc. are implemented.
  • QtWidgets: Contains most of the widgets available in Pyqt5.
  • QtGui: Contains GUI components and extends the QtCore module.
  • QtNetwork: Contains classes used to implement network programming through Qt. It supports TCP servers, TCP sockets, UDP sockets, SSL handling, network sessions, and DNS lookups.
  • QtMultimedia provides low-level multimedia functionality.
  • QtSql: implements database integration for SQL databases. Supports ODBC, MySQL, Oracle, SQLite, and PostgreSQL.

The only function that needs any explanation at this point is the addWidget() method. It is used to insert widgets into the HBox or VBox layout. It is also used in other layouts where it takes a different number of parameters as you will see in the next section. The widgets will appear inside the layout in the order in which you insert them.

QGridLayout

QGridLayout is used to create interfaces in which the widgets are laid out in the form of a grid (like a matrix or 2D array). To insert elements in a grid layout, you can use the matrix representation to define the number of rows and columns in the grid as well as the position of those elements.

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon