Introduction

In this tutorial, we will cover the steps to create a simple GUI (Graphical User Interface) application using PySide6. We will create a simple window with a button and a label. When the button is clicked, the label will display a message. First, it is necessary to install PySide6. Installation instructions can be found here.

For this tutorial, we will only use the widgets inherited from the QtWidgets class and the Slot class from the QtCore module. As a starting point let's create a new Python file simple_gui.py and import the necessary modules:

simple_gui.py
from PySide6 import QtWidgets
from PySide6.QtCore import Slot

QtWidgets contains all the classes that provide a set of elements to create a graphical user interface, such as buttons, labels and layouts. Some of the most common classes from QtWidgets are documented in the API reference. We will use the import statements above as a starting point for our code and build upon it. We will build simple_gui.py in three steps:

  • 1. Create an empty window
  • 2. Add the button and label widgets to the window
  • 3. Connect the button to a function that changes the label text

Note

Code snippets in this tutorial are added to simple_gui.py in every step. The final code can be found at the end of the tutorial. Code snippets that are added to the script will be highlighted in the code block like this:

example.py
# This code was already in example.py
message = "muhahahaha"

# This code is added to example.py in this step
print(message)

Note

After every one of the three steps, the progress in the GUI will be shown (i.e. the result of running simple_gui.py). The window that is displayed may look slightly different depending on your operating system. The screenshots in this tutorial were taken on a Windows machine.