Your first Flet app
Last updated
This module is optional, and it stands on its own: you can do it, the tkinter module, both or neither. It puts a modern app in front of the library system from the capstone, using Flet. Flet draws with Flutter, the toolkit behind many phone apps, so your app looks current from the first line. In this lesson you'll install it and open your first app.
By the end of this lesson you can
- Install Flet into the project's virtual environment
- Write a Flet app: a
mainfunction that's handed aPage - Explain who runs the event loop in a Flet app
- Spot code written for Flet before version 1
What Flet is
Flet lets you write an app in Python and have it drawn by Flutter, Google's toolkit for apps on phones, desktops and the web. You never write Flutter's own language: you build the screen out of Python objects, and Flet hands them to Flutter to draw.
Why use it:
- It looks modern straight away. Flutter follows Material Design, with rounded controls, smooth movement, and light and dark themes.
- Every control is a Python dataclass (lesson 3.6). You build screens out of the kind of class you've been writing since Stage 3, and make your own controls by subclassing.
- The same code runs as a desktop app or in a web browser.
- It's free: Flet is under the Apache 2.0 licence, and Flutter under a BSD licence. Both let any government or business use them.
One warning before you start. Flet 1.0 came out in 2026, and it changed a lot. Most tutorials and answers online are older, and their code won't run. Step 4 shows what that looks like. This module follows Flet 1.
Install it
Windows can't open in this page, so this module runs on your computer, in the project folder from the capstone. Turn on its virtual environment, as lesson 2.9 showed, and install Flet:
.venv\Scripts\activate # on macOS or Linux: source .venv/bin/activate python -m pip install "flet[all]"
flet[all] means Flet with all its extras: the part that opens desktop windows, the part that serves a web app, and the flet command-line tool. The quotes stop some terminals from reading the square brackets themselves.
Flet moves quickly, so write down the version you installed: python -m pip show flet prints it. Add it to pyproject.toml's dependencies, like "flet[all]>=1.1", so the next person gets a version this module works with.
Your first app
Save this as hello.py, in a new folder or anywhere outside library/. This module's Flet code was checked with Flet 1.1.4 on Windows 11.
import flet as ft def main(page: ft.Page) -> None: page.title = "Library desk" page.add(ft.Text("Welcome to the library")) page.add(ft.Button(content="Say hello", on_click=lambda e: page.add(ft.Text("Hello!")))) ft.run(main)
python hello.py
hello.py with Flet 1.1.4 on Windows 11. It’s dark because Windows is set to dark mode: Flet follows the system unless you choose (lesson F.2).A window opens with a line of text and a button. Press the button a few times: a new Hello! appears each time. Close the window to stop the program.
mainis your app. Flet calls it once, with aPage: the window's content, ready to fill.ft.Text(...)andft.Button(...)make controls, Flet's word for widgets.page.addputs them on the page, one under another.on_click=is the function to call when the button's clicked. Flet calls it with one argument, an event, which thislambda(lesson 2.5) ignores.ft.run(main)starts it all. It opens the window, callsmain, then runs the event loop: it waits for clicks and calls their handlers, until the window closes.
After each handler returns, Flet redraws whatever it changed. That's why the new Hello! appears without any extra code.
Code from an old tutorial
The wrong way. Here's the first app with one line copied from a tutorial written before Flet 1:
import flet as ft def main(page: ft.Page) -> None: page.title = "Library desk" page.add(ft.Text("Welcome to the library")) page.add(ft.ElevatedButton(text="Say hello")) ft.run(main)
python hello.py Unhandled error in main() handler Traceback (most recent call last): ... File "hello.py", line 7, in main page.add(ft.ElevatedButton(text="Say hello")) ^^^^^^^^^^^^^^^^^ ... AttributeError: module 'flet' has no attribute 'ElevatedButton'
Flet catches the error, prints it in the terminal under Unhandled error in main() handler, and shows it in the window too, so the app doesn't just vanish:
main raises an error.ElevatedButton was a button in older Flet. Flet 1 has ft.Button, and its label is content=, not text=.
So when an example online won't run, look at its age first. Clues that code is from before Flet 1:
ft.app(target=main)to start, instead offt.run(main)ft.ElevatedButton, or a button'stext=- a custom control that writes its own
__init__and callssuper().__init__(), which F.4 does a different way
The fix is to check each name against Flet's own documentation, which always shows the current version.
Check
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Flet: Documentation
- Flet: Flet 1.0: what changed from the versions before 1.0
- Python Packaging User Guide: Installing packages using pip and virtual environments