PyQt & PySide: Build Desktop GUI Apps with Python

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction

PyQt and PySide are powerful libraries for building cross-platform graphical user interfaces in Python using the Qt framework. They give you access to Qt's rich toolkit: widgets, event systems, styles, animations, database integration, and even QML for building modern, fluid interfaces.

The main difference between PyQt and PySide comes down to licensing. PyQt requires a GPL or commercial license, while PySide (the official Qt Company implementation) is distributed under the more permissive LGPL, making it the preferred choice for commercial projects.

In this comprehensive guide, we'll cover everything from installation and building simple interfaces to advanced projects with databases, custom styling, and packaging your finished applications.

Installation & Setup

Installing PyQt5 and PyQt6

# PyQt5 (stable version)
pip install pyqt5 pyqt5-tools

# PyQt6 (latest version)
pip install pyqt6 pyqt6-tools

Installing PySide2 and PySide6

# PySide2 (for Qt 5)
pip install pyside2

# PySide6 (recommended)
pip install pyside6

Verify Your Installation

# For PyQt5
from PyQt5.QtWidgets import QApplication
print("PyQt5 installed successfully")

# For PySide6
from PySide6.QtWidgets import QApplication
print("PySide6 installed successfully")

Building Your First App

A Simple Window with a Button

PyQt5 Version

from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
import sys

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('My First PyQt5 App')
        self.setGeometry(100, 100, 300, 200)
        
        layout = QVBoxLayout()
        button = QPushButton('Click Me')
        button.clicked.connect(self.on_click)
        layout.addWidget(button)
        
        self.setLayout(layout)
    
    def on_click(self):
        print("Button clicked!")

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

PySide6 Version

from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
import sys

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('My First PySide6 App')
        self.setGeometry(100, 100, 300, 200)
        
        layout = QVBoxLayout()
        button = QPushButton('Click Me')
        button.clicked.connect(self.on_click)
        layout.addWidget(button)
        
        self.setLayout(layout)
    
    def on_click(self):
        print("Button clicked!")

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Qt Application Architecture

Core Components

QApplication is the central object of any Qt application. It manages the event loop, handles system events, and coordinates all your widgets.

QWidget is the base class for all user interface objects. It can contain other widgets and act as a container.

QMainWindow is a specialized widget for creating the main application window, with built-in support for menus, toolbars, a status bar, and a central widget.

QDialog is the class for creating dialog windows, including both modal and non-modal dialogs.

Application Lifecycle

  1. Create a QApplication instance
  2. Create and configure your widgets
  3. Show the main window
  4. Start the event loop with exec() or exec_()
  5. Exit the application

Recommendations