PyQt5 supports a window status bar. This is a small bar at the bottom of a window that sometimes appears, it can contain text messages. To add a status bar add the line:
A statusbar can be added to the main window (QMainWindow). It is one of the methods the class itself contains;
Create GUI Apps with PyQt5
PyQt5 statusbar example:
The program below adds a statusbar to a PyQt5 window:
The example creates a window (QMainWindow). We set the screen parameters using:
Window properties are set in the initUI() method which is called in the constructor. The method:
sets the text on the statusbar.
Create GUI Apps with PyQt5
PyQt5 absolute positioning
Absolute positioning gives you total control over the widget positions but you have to explicitly define every widget location.
Widgets can be added on an absolute position using the move(x,y) method.
PyQt5 widget positioning example
The example below puts widgets on the absolute positions using the move() method.
They are added to a PyQT5 window (QMainWindow) which has some properties set in initUI().
In this article you will learn to use tabs with PyQt5. We’ll show the full code first, then explain. PyQt5 has a widget to create tabs known as QTabWidget. The QTabWidget can contain tabs (QWidgets), which have widgets on them such as labels, buttons, images etcetera.
PyQt5 tabs example
Full PyQt5 tabs example:
To add a table to a window, we create a new class:
We initialize the tab screen by creating a QTabWidget and two QWidgets for tabs.
We then add these tabs to the tab widget:
The contents of a tab are created using:
Finally we add the tabs to the widget:
don’t forget the to add your custom tab widget to the window:
A window can contain widgets (buttons, text field, image and others). Widgets are frequently added to a layout. A horizontal layout can be used to (dynamically) add widgets in horizontal direction.
In this article we’ll show you how to add buttons in the horizontal direction using a horizontal layout.
Horizontal layout example
We will show the whole code and then explain.
We start by calling the method self.createHorizontalLayout() inside the initUI() method.
Inside the method we create a box with a title and a horizontal layout:
We create widgets (in this example QPushButtons) and add them to the layout one by one:
We set the title-box to contain the horizontal layout:
In the initUI method we add it to the window:
You can paint in a PyQt5 window using the QPainter widget. This widget supports adding pixels (dots) inside of the widget, unlike the other widgets. In this article we’ll explain how to use the QPainter widget with Python.
To use the widget in Qt5 we import PyQt5.QtGui. This also contains other classes like QPen and QColor.
QPainter Widget Example
We set the window background using:
Pixels are added using the drawPoint(x, y) method.
PyQt5 QPainter example
The example below paints pixels in the QPainter widget:
PyQt5 supports a color picker known as QColorDialog. This dialog is a typical dialog that you would see in a paint or graphics program.
To get a color from PyQt5 dialog simply call:
PyQt5 color dialog example
The example below opens a QColorDialog after clicking the button, and returns the selected color.