TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python / Software Development

How To Create a Python GUI App With PyQt5

This tutorial shows you how to use the PyQt5 library to create GUIs. While Qt is a set of cross-platform C++ libraries that make it possible for Python to access features of modern desktop operating systems, the PyQt5 library also makes it possible to access mobile features, such as positioning services, NFC, Bluetooth, and more.
Jun 5th, 2024 5:00am by
Featued image for: How To Create a Python GUI App With PyQt5

Although Python is most often thought of as a language for creating text-based apps, it can (thanks to the help of certain libraries) create GUIs. One such library is PyQt5.

For those who are just getting started with Python, Qt is a set of cross-platform C++ libraries that make it possible for Python to access features of modern desktop operating systems (such as GUIs). This library also makes it possible to access mobile features, such as positioning services, NFC, Bluetooth, and more.

We’re going to focus on the GUI portion of Qt. Speaking of which…

PyQt5 is the Python binding for Qt v5 and includes several extension modules so developers can create user-friendly GUI applications.

I’m going to first walk you through the process of getting PyQt5 installed and then show you how it’s used to create a rudimentary application with Python.

What You’ll Need

I’m going to demonstrate this on an instance of Ubuntu Desktop 22.04. If you’re using a non-Debian distribution, you’ll need to modify the installation process to match the package manager used by your OS of choice. You’ll also need a user with sudo privileges (for the installation only).

With that at the ready, let’s make some GUI goodness.

Installing the Necessary Software

Although Linux ships with Python3 installed, we’re going to install the full version of Python3. Log onto your desktop, open a terminal window, and then issue the command:

sudo apt-get install python3-full -y

Once you’ve taken care of that installation, you can then install PyQt5 with the command:

sudo apt-get install python3-pyqt5 -y

Creating a QUI Window

The first thing we’re going to do is create a blank GUI (to show you how everything is imported and called). The first line instructs Python what classes we’re using. That line of code looks like this:

from PyQt5.QtWidgets import QApplication, QWidget

We have to use sys.argv to initialize the Qt application, which is done via:

import sys

To create an instance of the QApplication class and pass the sys.argv command line arguments like this:

app = QApplication(sys.argv)

We now define our window using the QWidget function and make sure it is shown (because they are hidden by default) with:

window = QWidget()
window.show()

Finally, we start an event loop that will keep the window open:

app.exec()

An event loop is a construct in programming that waits for and dispatches events or messages within a program. Event loops are often used to handle events such as I/O operations, timers, and callbacks. With a GUI application, the press of a key, the click of a mouse, or even mouse movement generates an event. That event is then placed in the event queue. During each iteration of the queue, a check is run and if a waiting event is discovered, both the event and control are passed to the event handler to pass control back to the queue. In our example, QApplication is responsible for holding the event loop.

The entire app code looks like this:

from PyQt5.QtWidgets import QApplication, QWidget

import sys

app = QApplication(sys.argv)

window = QWidget()
window.show() 

app.exec()

If you run the above, you’ll see an empty window (Figure 1), which isn’t very handy.

Figure 1

We can certainly do more than this.

Let’s add a button to our window. For that, we need to add another library to our from line. That library is QPushButton and now our from line looks like this:

from PyQt5.QtWidgets import QApplication, QPushButton

The only other thing we have to change is our window declaration, from:

window = QWidget()

to

window = QPushButton("My Button")

Our new app looks like this:

from PyQt5.QtWidgets import QApplication, QPushButton

import sys

app = QApplication(sys.argv)

window = QPushButton("My Button")
window.show() 

app.exec()

Let’s create a simple Hello World app with a Qt GUI.

from PyQt5.QtWidgets import QWidget, QApplication

import sys

application = QApplication(sys.argv)

mainWindow = QWidget()

mainWindow.setGeometry(0, 0, 350, 400)
mainWindow.setWindowTitle('Hello World')

mainWindow.show()

application.exec()

When you run that, you’ll see two differences:

  • We’ve defined the window geometry with mainWindow.setGeometry(0, 0, 350, 400).
  • We’ve added a title with mainWindow.setWindowTitle(‘Hello World’).

Make It Interactive

So far, even if we push the button on our app, it doesn’t do anything. To make it interactive, we have to use what’s called slots and signals. A signal is a notification a widget emits when something happens (such as a button click). A slot is a function (or method) invoked after a widget emits a signal.

Let’s make our push-button app respond to a clicked signal. What it will do is send the output of the signal to the terminal window, so you can see it in action. The first four lines of our code should be familiar:

from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

import sys

application = QApplication(sys.argv)

mainWindow = QWidget()

Next, we use the setGeometry() function to define the geometry (which will be 200 x 200 pixels) of our app with the line:

mainWindow.setGeometry(0, 0, 500, 500)

Set a title with:

mainWindow.setWindowTitle('Click Me New Stack')

We now define a function to create the slot with:

def clickedSlot():
    print("You've pushed my button!")

Our slot will send the message, “You’ve pushed my button” to the terminal output.

Create our button and then call the connect(clickedSlot) function with:

pushButton = QPushButton(parent=mainWindow, text='Click Here')
pushButton.clicked.connect(clickedSlot)

Make sure our window is visible and execute with the final two lines:

mainWindow.show()
application.exec()

Create a new file with:

nano pushme.py

Paste the full app code into the file, which is:

from PyQt5.QtWidgets import QWidget, QApplication, QPushButton

import sys

application = QApplication(sys.argv)

mainWindow = QWidget()
mainWindow.setGeometry(0, 0, 500, 500)
mainWindow.setWindowTitle('Click Me New Stack')

def clickedSlot():
    print("You've pushed my button")

pushButton = QPushButton(parent=mainWindow, text='Click Here')
pushButton.clicked.connect(clickedSlot)

mainWindow.show()
application.exec()

When you run the app (with python3 pushme.py), you’ll see our app window with the Click Here button. If you click the button, you’ll see You’ve pushed my button in the terminal window output (Figure 2).

Figure 2

The button has been pushed.

Congratulations, you’ve taken your first steps in creating a Python GUI with PyQt5. The next time around we’ll create a GUI app that’s actually useful.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.