Python | Introduction to PyQt5

Last Updated : 27 Jul, 2026

PyQt5 is a Python library used for building desktop graphical user interface (GUI) applications. It provides Python bindings for the Qt5 framework, allowing developers to create modern, interactive, and cross-platform applications using Python. It includes a rich collection of widgets, layouts, dialogs, menus, and other GUI components for developing everything from simple utilities to large desktop applications.

Installation

Before using PyQt5, install it using the following command in the command prompt or terminal:

pip install PyQt5

Qt Designer (Optional)

PyQt5 applications can be designed either by writing code manually or by using Qt Designer, a visual drag-and-drop interface designer. Qt Designer allows you to create windows, buttons, menus, dialogs, and other GUI components without writing the layout code manually.

To install the Qt Designer tools, run:

pip install pyqt5-tools

Note: pyqt5-tools is optional and may not be available for every Python version. If it is unavailable, you can still build complete PyQt5 applications by creating the interface directly in Python code.

Creating First PyQt5 Application

After installing PyQt5, you can create your first GUI application. The example below creates a simple window containing a button and a label. When the button is clicked, the label displays the message "You clicked me!".

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

class MyWindow(QWidget):

    def __init__(self):
        super().__init__()

        self.setWindowTitle("My First PyQt5 App")
        self.resize(300, 150)
        self.label = QLabel("")
        self.button = QPushButton("Click Me")
        self.button.clicked.connect(self.show_message)

        layout = QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.label)
        self.setLayout(layout)

    def show_message(self):
        self.label.setText("You clicked me!")

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

Output

Screenshot-2026-07-13-163204
Open the application window and click the Click Me button.

Explanation:

  • Creates a custom window by inheriting from the QWidget class.
  • Adds a QPushButton and a QLabel to the window using a vertical layout.
  • Connects the button's clicked signal to the show_message() method using clicked.connect().
  • Updates the label text to "You clicked me!" whenever the button is clicked.
  • Starts the application by creating a QApplication object and displaying the window.

Common PyQt5 Widgets

PyQt5 provides a large collection of built-in widgets for creating graphical user interfaces. These widgets allow users to display information, enter data, and interact with the application.

WidgetDescription
QLabelDisplays text or images.
QPushButtonCreates a clickable button.
QLineEditAccepts single-line text input.
QTextEditProvides a multi-line text editor.
QCheckBoxCreates a checkbox for multiple selections.
QRadioButtonAllows users to select one option from a group.
QComboBoxDisplays a drop-down list of items.
QListWidgetDisplays a selectable list of items.
QSpinBoxAllows users to select numeric values using arrows.
QProgressBarDisplays the progress of a task.

Using Layouts

Layouts automatically arrange widgets inside a window and adjust their positions when the window is resized. Instead of manually specifying widget positions, layouts help create responsive and organized user interfaces.

Python
import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget,
    QLabel, QPushButton,
    QVBoxLayout
)

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle("Vertical Layout")

layout = QVBoxLayout()

layout.addWidget(QLabel("Welcome to PyQt5"))
layout.addWidget(QPushButton("Button 1"))
layout.addWidget(QPushButton("Button 2"))
window.setLayout(layout)

window.show()
sys.exit(app.exec_())

Output

Screenshot-2026-07-13-163801
The application displays a window containing one label followed by two vertically arranged buttons.

Explanation:

  • Creates a QVBoxLayout object.
  • Adds widgets to the layout using addWidget().
  • Assigns the layout to the window using setLayout().
  • Automatically aligns widgets vertically and resizes them when the window size changes.

Signals and Slots

PyQt5 uses the Signals and Slots mechanism to handle user interactions. A signal is emitted when an event occurs (such as clicking a button), while a slot is a Python function that is executed in response to that signal.

Syntax:

widget.signal.connect(slot_function)

Example:

button.clicked.connect(show_message)

Explanation:

  • clicked is the signal emitted when the button is clicked.
  • show_message is the slot (function) that executes after the click event occurs.

Handling User Input

PyQt5 provides input widgets that allow users to enter text, numbers, and other information. The example below reads text entered by the user and displays it when a button is clicked.

Python
import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget,
    QLabel, QPushButton,
    QLineEdit, QVBoxLayout
)

class Window(QWidget):

    def __init__(self):
        super().__init__()

        self.input = QLineEdit()
        self.label = QLabel("")
        self.button = QPushButton("Show Text")

        self.button.clicked.connect(self.display_text)

        layout = QVBoxLayout()
        layout.addWidget(self.input)
        layout.addWidget(self.button)
        layout.addWidget(self.label)

        self.setLayout(layout)

    def display_text(self):
        self.label.setText(self.input.text())

app = QApplication(sys.argv)

window = Window()
window.show()

sys.exit(app.exec_())

Output

Screenshot-2026-07-13-163956
Type text into the input box and click Show Text. The entered text is displayed below the button.

Explanation:

  • Creates a QLineEdit for text input.
  • Retrieves the entered text using text().
  • Displays the entered text inside a QLabel.
  • Uses a button click to trigger the update.

Applications

PyQt5 can be used to develop a wide variety of desktop applications.

  • Desktop Applications: Build complete desktop software with graphical interfaces.
  • Business Applications: Create inventory, billing, and management systems.
  • Data Visualization Tools: Display charts, tables, and analytics dashboards.
  • Media Applications: Develop image viewers, video players, and multimedia tools.
  • Scientific Software: Build applications for simulations, visualization, and research.
  • Utility Tools: Create calculators, text editors, file explorers, and automation tools.
Comment