pyqt5 hello world

Make a graphical interface with PyQt?

Graphical interfaces made with PyQt run on: Microsoft Windows, Apple Mac OS X and Linux. We will create a Hello World app with PyQt.

pyqt hello world

Related course:

Install PyQt

PyQt is available for both Python 2 (2.7.9 tested) and Python 3.

To install write:

pip3 install pyqt5

With apt-get you can use:


PyQt hello world

The app we write will show the message “Hello World” in a graphical window. Import the PyQt5 module.

The program starts in the main part.

We initialize Qt and create an object of the type HelloWindow.

We call the show() method to display the window.

The HelloWindow class inherits from the class QMainWindow.

We call its super method to initialize the window.

Several class variables are set: size and window title.

We add widgets to the window, including a label widget (QLabel) which displays the message “Hello World”.

pyqt5 message box

When creating a Python GUI, you may want to show a message box at some point.

Pyqt comes with messagebox support in both PyQt4 and PyQt5. The class to be used is QMessageBox.

In this tutorial you’ll learn how to show a message box on buton click.

qt message box

Related course:

QMessageBox example

Import QMessageBox from the PyQt5 widgets

A messagebox can easily be added to the window using the code:

Complete code:

pyqt5 tooltip

A tooltip is a hint in a graphical interface.

Tooltips frequently appear when hovering the mouse over a widget (without clicking). Pyqt supports tool tips, they can be configured for widgets.

pyqt tooltip

Related course:

Note: Tooltips are not shown on mobile devices because there’s no mouse cursor.

Pyqt5 tooltip

A tooltip can be set using a widgets setTooltip method.

The setToolTip() method sets the tooltip message for a QtWidget.

Copy/paste example:

PyQt line edit

In this tutorial we’ll make an app that displays an input field.

A textbox or LineEdit can be created using the QLineEdit class. Many applications have text inputs like form fields and the like.


Related course:


Start by importing the QLineEdit widget:

We will also add a text label, to show the user what to type.
Import QLabel:

Then add both to the screen:

The text value can be printed using:

QLineEdit Example

We start with the code followed by the explanation.


pyqt5 sliders

PyQt5 comes with sliders out of the box.

Sliders can be either vertical or horizontal. We can connect them with a method the same way as we do with buttons.

A slider is often used in conjunction with a spinbox, QSpinBox. In this example we won’t use a spinbox, as that’s optional. You can connect a slider to any widget or functionality you want.

Related course:

Slider example
We create a groupbox which contains 4 sliders. A slider is created with the class QSlider, which accepts the flags Qt.Horizontal or Qt.Vertical.

pyqt sliders

If you want to add a slider to your existing code, don’t forget to import QSlider from PyQt5.QtWidgets.


pyqt textarea

QPlainTextEdit is a multiline text area in PyQt. To set the text, we use its method insertPlainText(). We can set its position and size using the methods move() and resize().

pyqt text area

Related course:


The example below creates a text area using PyQt5.

We will create the usual QMainWindow to add the widget to. It is just for plain text, like notepad. To add new lines we add the \n character.

pyqt checkbox

A checkbox can be created using the QCheckBox widget. When creating an new checkbox with the QCheckBox class, the first parameter is the label.

To apply actions to the toggle switch, we call .stateChanged.connect() followed by a callback method. When this method is called, it sends a boolean as state parameter. If checked, its the value QtCore.Qt.checked.

checkbox pyqt

Related course:

Checkbox example

The example below creates a checkbox which calls the method clickBox when toggled.

pyqt combobox

A combobox can be created with the QComboBox class. Items are added using the class method addItem, followed by a string parameter.

Sometimes its called a listbox, as it shows a list of items a user can select from. If you want a user to choose from a set of items, the listbox is the best option.

pyqt combobox

Related course:

Combobox example

The code below creates a combobox using the class QComboBox.

pyqtgraph bar chart

The module pyqtgraph supports all kinds of charts and scientific graphics. Underneath, pyqtgraph uses PyQt4 / PySide and numpy. Its been tested to work on Linux, Windows, and OSX.

In this article we’ll create an example bar chart. The output will be this awesome chart:

pyqtgraph barchart

pyqtgraph barchart

We create the data to plot (x y1) first. y1 is a list of 20 floats that we create using the numpy method linspace.

Then create the barchart using the method BarGraphItem(), where the parameters are x, y, bar width and color.