Lesson 1
Install PyQt and create your first window
Understand the minimum structure of a PyQt application.
Theory
PyQt is not built into Python like Tkinter. Students usually install it with pip, then create a QApplication and at least one visible widget. The event loop starts with app.exec().
Key points
- Install PyQt6 with pip.
- QApplication represents the running GUI app.
- QWidget creates a blank window.
- show() makes the window visible.
- app.exec() starts the event loop.
Classroom note
Explain that QApplication is like the engine of the app. QWidget is the visible car body. app.exec() keeps the engine running.
# Install first:
# pip install PyQt6
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("My First PyQt App")
window.resize(420, 240)
label = QLabel("My First PyQt App")
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout = QVBoxLayout()
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec())
This image was generated by executing the PyQt example and capturing the application window.












