Home / Guides / Example

PyQt Example: Build Your First Real App

A complete PyQt example — a working temperature converter with a window class, layouts, an input, a button and live results. Copy it, run it and learn from it.

EXAMPLE

PyQt Example: Build Your First Real App

The best way to learn PyQt is to build something small and complete. Here is a full example — a temperature converter — with every part explained so you can reuse the pattern in your own apps.

What we are building

We will build a small app that turns Celsius into Fahrenheit. The user types a number, presses a button, and sees the result. It is tiny, but it uses the same parts as a large app: a window, a layout, an input, a button and a slot.

Once this clicks, you can swap the maths for anything — currency, units, tax — and the shape stays the same.

Start with a window class

Real apps subclass QWidget. This keeps the interface and the behaviour together, which scales far better than loose variables. Our class sets a title and size in its constructor.

Build the interface

We need three things on screen: a box to type in, a button to press, and a label for the answer. A vertical layout stacks them neatly, so the window looks right at any size.

Add the logic

When the button is clicked, we read the text, convert the number, and update the label. We also guard against bad input, so the app never crashes if someone types letters.

The full example

converter.py
import sys
from PyQt6.QtWidgets import (QApplication, QWidget, QLabel,
    QLineEdit, QPushButton, QVBoxLayout)

class Converter(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Temp Converter")
        self.resize(320, 180)

        self.input = QLineEdit()
        self.input.setPlaceholderText("Celsius")
        self.button = QPushButton("Convert")
        self.result = QLabel("Result will show here")

        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("Enter Celsius:"))
        layout.addWidget(self.input)
        layout.addWidget(self.button)
        layout.addWidget(self.result)

        self.button.clicked.connect(self.convert)

    def convert(self):
        try:
            c = float(self.input.text())
            f = c * 9 / 5 + 32
            self.result.setText(f"{c} C = {f:.1f} F")
        except ValueError:
            self.result.setText("Please enter a number")

app = QApplication(sys.argv)
win = Converter()
win.show()
sys.exit(app.exec())

Run and extend it

terminal
python converter.py

Type a number, press Convert, and the answer updates at once. Now make it yours. Add a second button for Fahrenheit to Celsius. Add a dropdown to pick units. Style it with a Qt stylesheet. Each small change teaches a new corner of PyQt.

Notice how self.button.clicked.connect(self.convert) links the click to a method. That one line is the engine of the whole app.

Add a reset button

A great way to learn is to extend the converter. Add a second button that clears the input and the result. You create the button, add it to the layout, and connect its click to a small method that empties both fields.

This tiny change reinforces the core loop: make a widget, place it, connect a signal. Once you can add a feature like this without looking anything up, the pattern has truly sunk in.

Give it some style

The converter works, but it looks bare. A short Qt style sheet lifts it instantly. Give the window a soft background, round the button corners, and bump up the result font so the answer stands out.

Because style and logic are separate, you can experiment freely. Change a colour, run the app, and see the effect at once. This fast feedback loop makes styling genuinely fun rather than a chore.

Where to take it next

From here the app can grow in any direction. Add a dropdown to switch between temperature, length and weight. Save the last value so it is there next time. Add a menu bar with an About box.

Each addition teaches a new widget or idea while keeping the familiar shape underneath. That is the real lesson: once the pattern is yours, every new feature is just another small, confident step.

Handling input safely

Real users type unexpected things. Our converter already guards against this with a try/except block. If the input is not a number, it shows a friendly message instead of crashing. This small habit separates a toy from a tool.

You can take it further. Trim spaces from the input before converting. Show the message in a soft colour so it reads as a hint, not an error. Clear the message once the user types a valid number. Each touch makes the app feel considered.

Good input handling is invisible when it works, and sorely missed when it does not. Build the habit early and every app you write will feel more solid.

Turning it into a template

The converter's shape is reusable. A window class, a layout, some inputs, a button, and a slot that reads the inputs and updates a result. Swap the maths and you have a different tool with the same bones.

Try it. Replace the temperature formula with a currency rate, or a tip calculator, or a BMI check. You will find yourself changing only a few lines, because the structure already does the heavy lifting.

Saving this pattern as a personal starter template pays off for years. Every time an idea strikes, you begin from a working app instead of a blank file.

Key takeaways

This converter is small, but it carries the full shape of a real app: a window class, a layout, inputs, a button, a slot and safe error handling. Learn this pattern once and you can build an endless variety of tools from the same bones.

The engine of it all is a single line — connecting the button's click to a method. That link between a signal and your function is what makes any PyQt app interactive. Everything else is just deciding what the function should do.

Keep extending it. Add a reset button, a dropdown, a style sheet, a second conversion. Each change is small, each one works, and each one teaches a new corner of PyQt while the familiar structure holds it all together.

FAQs

Why use a class instead of plain functions?

A class keeps related widgets and methods together. It is much easier to grow than scattered variables.

What does the try/except do?

It catches bad input. If the user types text instead of a number, the app shows a friendly message rather than crashing.

Can I turn this into an EXE?

Yes. Our packaging guide shows how to build a single file others can run.

How do I add more screens?

Create more widgets or windows and switch between them with a stacked layout or new dialogs.

Conclusion

You just built a complete PyQt app in about thirty lines. It has an input, a button, live output and safe error handling — the same bones every real app shares. Keep going with more widgets, tidy your screens with layouts, then ship it as an executable.

Ready to start? Download PyQt or run pip install PyQt6.