GUI application development using PyQt in python

GUI application development using PyQt in python

tudip-logo

Tudip

30 July 2019

PyQt is a GUI module for the Python programming language. It allows users to create programs with a good user interface and simply and easily.

It is implemented as a Python extension module it contains Pyqt GUI library, which is written in C++.

Pyqt is powerful, looks native on all the major platforms, and has probably the biggest community. PyQt implements the most popular Qt library, and so if you are familiar with Qt development process of creating the application in another language, you may already be familiar with Qt. Pyqt module creates the possibility of developing applications in a unique and simple way.

In python there are two different bindings present: PyQt is older and more mature, but it’s only free if your application is open source, while PySide is newer and more permissively licensed (LGPL). I have been referring the main Qt docs a lot and other document related to the module – but both PyQt and PySide’s docs contains useful information.

You should have a basic understanding of some of the basic concepts, when it comes to GUI programming, with Python and Qt. In Pyqt module, I intend to put what we learned so far to use but also want to show you some new things related to this module that will make your application more user-friendly. Speaking of the application, in this pyqt module, we are going to be writing a GUI application from scratch, and I thought it might be a good idea to write a simple script in the form of application.

You will need Python 2.6+ or newer. Try one of these commands in order to test your Python version.

python3 --version
python --version

Installation process of PyQt

In python, the best way to manage dependencies is via a virtual environment. A virtual environment is simply a directory that contains the libraries for a specific project. Means in this directory we can add the dependencies, packages for some specific project. This is unlike a system-wide installation of those libraries, which would affect all of your other projects as well.

Execute the following command in order to create a virtual environment in the current directory

python3 -m venv venv

On Mac and Linux, use:

source venv/bin/activate

To now install PyQt, issue the following command:

pip install PyQt5

PyQt5 window

For installing the Pyqt5, you should install it first.

In a terminal you can type:

sudo apt-get install python3-pyqt5

Normally this procedure will work, but some time PyQt5 can give some issues on some specific platforms. If you can not find solution related to your problems, the alternative option is to install Anaconda version of python which will have all the packages already available on all the platforms.

To test whether PyQt5 is working properly or not, we can create a small script of code with the following:

from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication([])
win = QMainWindow()
win.show()
app.exit(app.exec_())

If you run this script of code, a small, empty window should pop up on the screen. This means that everything is working correctly.

Widget

In PyQt the important building blocks of windows are called Widgets. A window is a widget, a grid, a button, image, icon, etc. You can even define your own custom widgets in your own ways. In the existing code, you see that there is only an empty window appearing, not too exciting.

Let’s add a button to the window:

from PyQt5.QtWidgets import QPushButton , QApplication, QMainWindow,
app = QApplication([])
win = QMainWindow()
button = QPushButton('Hello World')
win.setCentralWidget(button)
win.show()
app.exit(app.exec_())

In PyQT modules, buttons are important aspects. In PyQt Buttons are called QPushButton. Apart from adding the widget, the code will be the same, like the creation of the app, or the execution of the loop. When we need to create a button, we also define the text that the button will have. You can add a button to the window by using different options. In this case, since we defined the window as a QMainWindow, we can set the button as its central widget.

Layouts

Like the example above, your GUI consists of multiple widgets. In this case, you need to tell Qt how to define the positioning of them. For example, you can use QVBoxLayout to stack widgets vertically:

QVBoxLayout example

from PyQt5.QtWidgets import QWidget, QPushButton, QApplication,QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
layout.addWidget(QPushButton('Bottom'))
layout.addWidget(QPushButton('Top'))
window.setLayout(layout)
window.show()
app.exec_()

As before, we instantiate a QApplication. Then, we create a window. We can use the most basic type of QWidget for it because it merely acts as a container and we don’t want it to define special behavior. Next, we create the layout and add two QPushButtons to it. Finally, we can tell the window to use this layout. As in our first sample application, we end with calls to .show() and app.exec_() for showing the application.

Custom colors

Pyqt module provides many features which allow to adding styling to the code.you can use QPalette and app.setPalette(…)

For example:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QPushButton

application = QApplication([])
application.setStyle('Fusion')
palette = QPalette()
palette.setColor(QPalette.ButtonText, Qt.green)
application.setPalette(palette)
button = QPushButton('Hello World')
button.show()
application.exec_()

This changes the text color in buttons to green.

Style sheets

pyqt module provides some styling functions which allows adding CSS properties. By applying the style sheets you can change the appearance of the application. We can use this for example of code for adding some spacing:

from PyQt5.QtWidgets import QPushButton, QApplication,
application = QApplication([])
application.setStyleSheet("QPushButton { margin: 15ex; }")
button = QPushButton('Hello World')
button.show()
application.exec_()

For further information, you can also visit PyQt official documentation.

search
Request a quote