No history yet

Introduction to PySide6

What is PySide6?

PySide6 is the official Python toolkit for building graphical user interfaces (GUIs) with the Qt framework. Think of it as a bridge that lets you use the powerful and versatile Qt library, originally written in C++, directly within your Python code.

Qt is used to build cross-platform applications, meaning the same code can run on Windows, macOS, and Linux with a native look and feel. From simple dialog boxes to complex software like video editors, Qt provides the building blocks, and PySide6 makes those blocks available to Python developers.

Lesson image

Getting Started

Before you can build an application, you need to install the PySide6 library. If you have Python and its package manager, pip, installed, you can add PySide6 to your environment with a single command in your terminal.

pip install PySide6

Once the installation is complete, you're ready to start coding. You can use any text editor or Integrated Development Environment (IDE) that supports Python, such as VS Code, PyCharm, or even Python's built-in IDLE.

Your First Application

Let's create the simplest possible desktop application: a window that displays the text "Hello, World!". This classic example introduces the essential components of any PySide6 app.

import sys
from PySide6.QtWidgets import QApplication, QWidget, QLabel

# 1. Create an instance of QApplication
app = QApplication(sys.argv)

# 2. Create the main window
window = QWidget()
window.setWindowTitle('Simple App')
window.setGeometry(100, 100, 280, 80) # x, y, width, height

# 3. Create a widget to display text
hello_msg = QLabel('<h1>Hello, World!</h1>', parent=window)
hello_msg.move(60, 15) # Position within the window

# 4. Show the window
window.show()

# 5. Start the application's event loop
sys.exit(app.exec())

Let's break that down.

Every PySide6 application needs one QApplication object. It handles background tasks and the event loop, which listens for user actions like mouse clicks and key presses.

The QWidget is the base for all user interface objects. Here, we use a plain QWidget as our main window. We set its title and size.

QLabel is a simple widget used for displaying text or images. We pass our window as the parent, which places the label inside the window.

Finally, window.show() makes the window visible on the screen, and app.exec() starts the event loop, keeping the application running until the user closes it.

This foundation is all you need to start building more complex interfaces.

Quiz Questions 1/5

What is the primary purpose of PySide6?

Quiz Questions 2/5

Which object is essential for managing the event loop and background processes in every PySide6 application?

Now that you have the basics down, you're ready to explore more advanced widgets and layouts.