TUTORIAL
New to PyQt? This tutorial takes you from a blank file to a working, interactive window. We explain each piece of a PyQt program as we go, in plain language, so the ideas stick.
The shape of every PyQt app
Every PyQt program has the same simple shape. You create one application object. You build some widgets. You show them. Then you start the event loop. Learn this shape once, and every example you read will make sense.
The event loop is the part that keeps your window alive. It waits for clicks, key presses and other events, then sends them to your code. Without it, the window would open and close in a flash.
Your first window
Here is the smallest useful PyQt program:
from PyQt6.QtWidgets import QApplication, QWidget
app = QApplication([])
win = QWidget()
win.setWindowTitle("Hello PyQt")
win.resize(360, 200)
win.show()
app.exec()Run it with python app.py. An empty window appears. QApplication runs the show. QWidget is a blank window. app.exec() starts the loop that keeps it open.
Adding widgets
Widgets are the parts you see — labels, buttons, text boxes. You place them inside a parent window:
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QPushButton
app = QApplication([])
win = QWidget()
label = QLabel("Click the button", win)
button = QPushButton("Click me", win)
button.move(0, 30)
win.show()
app.exec()This works, but moving each widget by hand is fragile. The moment the window resizes, things look wrong. Layouts solve that.
Laying things out
A layout places widgets for you and adjusts them when the window changes size. The vertical layout stacks items top to bottom:
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout
app = QApplication([])
win = QWidget()
layout = QVBoxLayout(win)
layout.addWidget(QLabel("Click the button"))
layout.addWidget(QPushButton("Click me"))
win.show()
app.exec()There are box, grid and form layouts for different jobs. Our layouts guide shows each one with examples.
Making it react
A button that does nothing is no fun. Connect its clicked signal to a function, and the app comes alive:
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout
app = QApplication([])
win = QWidget()
label = QLabel("Hello")
button = QPushButton("Change text")
button.clicked.connect(lambda: label.setText("You clicked!"))
lay = QVBoxLayout(win); lay.addWidget(label); lay.addWidget(button)
win.show(); app.exec()This link between a signal and a function is the heart of PyQt. Our signals and slots guide goes deeper.
Moving to a class
As apps grow, loose variables get messy. Real apps wrap the window in a class. This keeps the look and the behaviour in one place:
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
class Main(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout(self)
lay.addWidget(QLabel("Hello from a class"))
app = QApplication([])
win = Main(); win.show(); app.exec()From here, you can add methods for each button and keep everything tidy as the app grows.
Styling your window
Default PyQt windows look plain, and that is fine while you learn. When you want more polish, Qt style sheets let you change colours, fonts, padding and borders with CSS-like rules. You set them on a widget or on the whole app.
For example, you can give every button rounded corners and a brand colour in a few lines. Style sheets keep the look separate from the logic, so you can restyle an app without touching how it works.
Organising a bigger app
A ten-line demo fits in one file. A real app does not. As your project grows, split it into modules: one file for the main window, others for dialogs, and a separate file for any data handling.
Keep the user interface and the logic apart where you can. When a button is pressed, let its slot call a plain function that does the real work. That separation makes the code easier to read, test and change later.
Finding mistakes
When something does not work, run your script from a terminal rather than double-clicking it. Qt prints useful messages there, and a stray print() in a slot quickly shows whether it even ran.
Most early bugs fall into two groups: a signal connected to a function that was called by mistake, or a widget that was never added to a layout. Check those two first and you will solve a surprising share of problems.
A simple mental model
It helps to picture a PyQt app as three layers. At the bottom is the application object, which runs the event loop. In the middle are your widgets, arranged by layouts. On top sits your logic, connected to the widgets through signals and slots.
When a user clicks a button, the event travels up: the widget emits a signal, your slot runs, and it may change other widgets in response. Keeping this picture in mind makes new examples easy to follow, because you can always ask which layer a line belongs to.
Almost everything in PyQt fits this model. Once you see it, the toolkit stops feeling like a pile of classes and starts feeling like a tidy system you can reason about.
Three projects to practise
Reading only gets you so far. The fastest way to learn is to build small, finished things. Start with a countdown timer: a number, a start button, and a label that updates each second. It teaches timers and signals together.
Next, build a notes box that saves what you type to a file and loads it again on start. This covers text widgets and simple file handling. Finally, build a unit converter with a dropdown, which brings in choices and live updates.
Each project is small enough to finish in a sitting, yet each adds a new skill. Three little apps will teach you more than any amount of passive reading.
Key takeaways
Every PyQt program shares one shape: create the application, build widgets, arrange them with layouts, connect signals to slots, and run the event loop. Once that shape is familiar, every example you meet becomes easy to read.
Think in three layers — the app at the bottom, widgets in the middle, your logic on top. When something happens, the event travels up through those layers. Keeping that picture in mind turns a pile of classes into a system you understand.
Above all, learn by building. A timer, a notes box, a converter. Small finished projects teach far more than passive reading, because they force every idea to actually work. Build three little apps and you will be genuinely comfortable with PyQt.
FAQs
Do I need to know C++ to learn PyQt?
No. You write pure Python. Qt's C++ core runs underneath, but you never touch it.
Is PyQt good for beginners?
Yes, once you know basic Python. The shape is simple and the same every time.
Where can I practise?
Rebuild small tools — a timer, a notes box, a unit converter. Small wins teach the most.
Should I learn PyQt5 or PyQt6?
Learn PyQt6. The ideas carry over to PyQt5 if you ever need it.
Conclusion
You now know the core loop: make an app, build widgets, lay them out, connect a signal, and run. That pattern powers every PyQt program, big or small. Next, build a complete mini-app in our first example guide, then explore more widgets to fill out your toolbox.
Ready to start? Download PyQt or run pip install PyQt6.