PyQt5 GUI Tutorial: Create Python Desktop Apps Step by Step

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

What is PyQt5 and Why Use It?

PyQt5 is a set of Python bindings for the Qt library — one of the most powerful tools for building cross-platform graphical user interfaces. With PyQt5, developers can create professional desktop applications featuring modern design and rich functionality.

Key Features of PyQt5

PyQt5 offers a wide range of capabilities:

  • A vast collection of ready-to-use widgets via the QtWidgets module
  • Full support for windows, buttons, input fields, and lists
  • Signal and slot mechanism for event-driven programming
  • Integration with graphical elements through QGraphicsView
  • Customizable interfaces using stylesheets
  • Cross-platform compatibility across major operating systems

Installing PyQt5

Install PyQt5 using pip:

pip install PyQt5

Creating Your First Application Window

Every PyQt5 application starts with creating a QApplication object. This is the main application object that manages all windows and events. The main window is built on QWidget.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Simple Window")
window.resize(300, 200)
window.show()
sys.exit(app.exec_())

QApplication is mandatory for any PyQt5 GUI application. It handles system events and manages the application lifecycle.

Working with Core UI Controls

QLineEdit - Text Input Field

QLineEdit is a single-line text input field. This widget is widely used to capture user input.

from PyQt5.QtWidgets import QLineEdit, QVBoxLayout, QPushButton

window = QWidget()
window.setWindowTitle("QLineEdit Example")

layout = QVBoxLayout()

line_edit = QLineEdit()
line_edit.setPlaceholderText("Enter your name")

button = QPushButton("Submit")

def on_click():
    print(f"Hello, {line_edit.text()}!")

button.clicked.connect(on_click)

layout.addWidget(line_edit)
layout.addWidget(button)
window.setLayout(layout)
window.show()

The input field supports various methods for text manipulation:

  • setText() - programmatically set text
  • text() - retrieve the entered text
  • setPlaceholderText() - set placeholder text
  • setMaxLength() - limit maximum input length

QComboBox - Creating Dropdown Lists

QComboBox lets users select one option from a dropdown list.

from PyQt5.QtWidgets import QComboBox

combo = QComboBox()
combo.addItems(["Select an option", "Python", "C++", "JavaScript"])

def on_selection_change(value):
    print(f"You selected: {value}")

combo.currentTextChanged.connect(on_selection_change)

layout.addWidget(combo)

QComboBox provides several useful methods:

  • addItem() - add a single item
  • addItems() - add a list of items
  • currentText() - get the currently selected text
  • currentIndex() - get the index of the selected item
  • clear() - remove all items

QCheckBox - Working with Checkboxes and Signals

QCheckBox allows users to toggle an option on or off.

from PyQt5.QtWidgets import QCheckBox

checkbox = QCheckBox("I agree to the terms")

def on_checkbox_state(state):
    if state:
        print("Checkbox is checked")
    else:
        print("Checkbox is unchecked")

checkb                

Blogs

Book Recommendations