VOOZH about

URL: https://thenewstack.io/how-to-create-a-python-gui-app-with-pyqt5/

⇱ How To Create a Python GUI App With PyQt5 - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
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%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-06-05 05:00:01
How To Create a Python GUI App With PyQt5
tutorial,
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 Jack Wallen
👁 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

👁 Image

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

👁 Image

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.