INTRODUCTION TO
PYQT
Lecture 13
What is PyQt?
Python binding of the
Qt toolkit
Developed by
Riverbank Computing
Supports GUI
development using C+
+-based Qt in Python
PyQt is a bridge
between Python and Qt
Why Use PyQt?
Feature-rich and flexible GUI toolkit
Native look and feel across platforms
Vast widget library and customization
PyQt Versions
■ PyQt4 (legacy)
■ PyQt5 (widely used)
■ PyQt6 (modern, supports Qt6)
■ Compatibility and differences with Qt versions
Installing PyQt
■ Using pip: `pip install PyQt5`
■ Qt Designer via `pyqt5-tools`
■ Optionally use conda environment
Your First PyQt Application
Minimal app example:
QApplication and QWidget
QApplication
manages the
control flow
QWidget is the
base class for
all UI
components
All visual
elements derive
from QWidget
Layout
Management
■ Ensures widgets are placed and
resized correctly
Types:
■ QVBoxLayout, QHBoxLayout
■ QGridLayout, QFormLayout
Common
Widgets
Text:
Qlabel
QLineEdit
QTextEdit
Buttons:
QPushButton
QRadioButton
Selectors:
QComboBox
QListWidget
Signals and Slots
Signals =
events (e.g.,
button click)
Slots =
functions that
handle events
Automatic
connection via
`.connect()`
Connecting Signals to Slots
■ Slots can be regular functions or lambdas
Message Boxes
DISPLAY MESSAGES WITH
`QMESSAGEBOX`
TYPES: INFORMATION,
WARNING, CRITICAL, QUESTION
Menus and Toolbars
Use
`QMainWindow`
for menu and
toolbar support
Actions defined
via `QAction`
Dialogs and File
Choosers
■ Custom and built-in dialogs
■ Use
`QFileDialog.getOpenFileName()`
Designer Tool
■ Qt Designer = drag-and-drop UI creation
■ Save as `.ui` files
■ Convert with `pyuic5`:
– pyuic5 design.ui -o design.py
Custom Widgets and Styling
■ Subclass QWidget or other widgets
■ Override methods like `paintEvent()`
■ CSS-like styling using `setStyleSheet()`
■ button.setStyleSheet("background-color: lightgreen;")
BUILDING A PYQT
PROJECT
Step-by-step guide
Set Up Your Environment
1. Install Python (>= 3.7)
2. Create a virtual environment
3. Install PyQt: `pip install PyQt5`
4. Optionally install pyqt5-tools for Qt Designer
Project Structure
1. main.py (entry point)
2. ui/ (for .ui files)
3. resources/ (images, icons)
4. widgets/ (custom widgets)
5. controllers/ (logic handlers)
Design UI with Qt Designer
■ Use drag-and-drop interface
■ Save UI as .ui files
■ Convert with `pyuic5`: `pyuic5 main.ui -o main_ui.py`
■ Maintain separation of UI and logic
Create the Main Application
Use QApplication and main window:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
window = QMainWindow()
window.show()
sys.exit(app.exec_())
```
Connect UI to Logic
■ Import the generated `main_ui.py`
■ Connect signals to custom slots (functions)
■ Example:
```python
self.button.clicked.connect(self.handle_click)
```
Add Custom Widgets
■ Subclass QWidget or other widgets
■ Override methods like `paintEvent()`
■ Integrate with main UI through layout
Style with Qt Stylesheets
■ Use `setStyleSheet()` with CSS-like syntax
■ Define themes and reusable styles
Example:
```python
self.setStyleSheet('QPushButton { color: white; background:
blue; }')
```
Conclusion:
- Keep UI and logic separate
- Organize code into modules
- Use signals/slots efficiently
- Comment and document your code

Python_Lecture13_Introduction to PyQt.pptx