Python GUI

A GUI (graphical user interface) can be made with Python modules.There are lots of modules designed for that. One of the oldest ones is Tkinter, the standard graphical interface package.
Through the years more modern modules have come out: Qt and WxWidgets.

Related Course: Create Simple GUI Applications with Python and Qt

PyQT

The PyQT module works on all desktop platforms (Windows, Mac, Linux). It has a modern look and PyQt5 is the current version. QT includes GUI widgets, network sockets, SQL databases, a multimedia framework and many cool things.

qlineedit
A simple PyQt application.

Related Course: Create Simple GUI Applications with Python and Qt

Tkinter

An older module to create GUI applications. It works on Microsoft Windows and Mac OS X. This module contains less widgets than QT. It contains the basic widgets like buttons, textbox, listview and labels. If you are looking for advanced widgets or unusual widgets, I recommend QT or WxPython.

Related course: Tkinter GUI Application Development Projects

WxPython

This is an alternative to PyQT and Tkinter. The WxPython module uses a C++ library called WxWidgets. With this module you can create applications for Windows, Mac OS X, Linux. Applications have a native look on all platforms. Unlike PyQt, WxPython is not developed by a commercial enterprise.

PyQt

Python is not limited to making terminal applications. To create a graphical user interface (GUI) with Python, the module Pyqt can be used.

Applications made with Python and Pyqt work on Windows, Mac and Unix. There are two major versions of Pyqt: PyQt4 and PyQt5. We recommend going with the later version as the other is older.

Related course:

An example Python GUI (Pyqt) application:

pyqt tooltip
Popular apps made with Pyqt are: Konqueror, the KDE Desktop environment and many others.

To install Pyqt type pip3 install pyqt5 or pip install pyqt5.

Pyqt Articles
These articles form a short introduction to the PyQt5 module.

 

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:

python3-pyqt5

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 center window

To center a PyQt window, we need to do a bit of trickery: we need to get the window properties, center point and move it ourself. At the start of the program, it will be in the center of the screen.

Related course:

pyqt center screen

We add QDesktopWidget to the list of imports, having:

Include this in the window creation method:

The geometry of the window is then retrieved using:

We can output qtRectangle using print(qtRectangle),
this would return PyQt5.QtCore.QRect(0, 0, 640, 480)

Get the center of the screen using

Set it to the center

pyqt5 button

Add a button in pyqt?

Learn how to add button widgets to an existing Pyqt window. Learn how to connect a click to a Python method.

pyqt button

Related course:

Introduction

The button widget is called QPushButton.
Start by importing QPushButton

In the window constructor, add these lines

The first line creates an object of the type QPushButton.

We resize it to 100 pixels in width and 32 in height. Then we set it to position (50,50) on the window. The click must be linked to a Python method, clickMethod().

pyqt5 button example

Complete code resulting in:

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 menu

Do you want a menu in your PyQt app?

Pyqt has menu support. Almost every GUI app has a main menu at the top of the window. Adding a menu works slightly different than adding widgets.

pyqt menu

A menu can contain sub menus, they are usually something like (File, Edit, View, History, Help). Every menu has actions.

Related course:

Pyqt menu example

The menu is created with:

Then actions are added to the file menu:

These actions must be defined beforehand:

Complete code:

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.

QLineEdit

Related course:

Introduction

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 video widget

In this article you’ll see the code to create a video player with PyQT5.
It contains a menu bar for opening the video file.

pyqt video player

PyQt5 supports multimedia, including a video widget: QVideoWidget. This is used in combination with the class QMediaPlayer.

The QMediaPlayer class has all kinds of properties that you may know from video players: audioAvailable, duration, position, volume and a few others.

Related course:

Introduction

We create a window (QMainWindow) that contains a widget for its contents (QWidget).
Then we add the videowidget and control widgets.

The player will only play a video, in this example we don’t use back buttons, stop buttons.

QMediaPlayer Example

The complete code below: