Flet · lesson 2 of 6
Parts in this lesson
Aft.Column(controls=[...])4.15: composition
Bcount.value = ...change a field
Ccontrol.update()redraw it now
Dft.Theme(color_scheme_seed=...)colours from one seed

Controls are objects

Last updated

A Flet screen is a tree of objects: a column holds rows, a row holds buttons. Each control is a dataclass, and changing one of its fields changes the screen. In this lesson you'll lay out controls inside controls, change them from a handler, find out when Flet redraws, and give the app a colour theme with light and dark modes.

By the end of this lesson you can

  • Build a screen as a tree of controls with Column, Row and Container
  • Change a control's fields in a handler, and say when the screen shows it
  • Read the error from updating a control that isn't on the page yet
  • Set a colour theme and switch between light and dark

A tree of dataclasses

Every Flet control is a dataclass (lesson 3.6). Its fields are what it shows: a Text has a value, a Button has content, and a Column has controls, a list of the controls inside it. Controls inside controls make a tree, with the page at the top.

Flet can't run in this page, but its idea can. Here are four pretend controls, made the same way, and a function that prints the tree. Press Run:

Step 1 code tree.py
Edit the code, then run it again
  • The Column has a Text and a Row, and the Row has two buttons. That's composition (lesson 4.15), and it's how every Flet screen is built.
  • field(default_factory=list) gives each column its own new list. Lesson F.4 shows what goes wrong without it.
  • Changing a field is plain Python: screen.controls[0].value = .... In Flet, that's how you change what's on the screen.

Rows, columns and containers

Flet's real layout controls work like the pretend ones:

ControlWhat it does
ft.Column(controls=[...])puts its controls one under another
ft.Row(controls=[...])puts its controls side by side
ft.Container(content=...)holds one control, with padding, a colour or a border around it

Save this as counter.py and run it:

import flet as ft


def main(page: ft.Page) -> None:
    page.title = "Library desk"
    count = ft.Text("Nothing lent yet", size=20)
    lent = 0

    def lend(e: ft.Event[ft.Button]) -> None:
        nonlocal lent  # lent belongs to main, not to lend
        lent += 1
        count.value = f"Lent today: {lent}"

    page.add(
        ft.Container(
            padding=20,
            content=ft.Column(
                controls=[count, ft.Row(controls=[ft.Button(content="Lend one", on_click=lend)])]
            ),
        )
    )


ft.run(main)
A dark window with large text Nothing lent yet above a Lend one button
counter.py before any clicks.

Press Lend one, and the text counts. Read it from the outside in: a container with 20 pixels of padding holds a column, which holds the text and a row with the button.

lend changes count.value, a field of the Text. Flet sees the change and redraws the text once lend returns. The number itself lives in lent, a variable of main. A function inside another can read its variables, but to change one it must say nonlocal lent first; without that, lent += 1 would make a new local variable and fail. F.4 keeps state in a class instead, which is tidier.

When the screen changes

Flet redraws after a handler returns. Sometimes you want it sooner: say, to show Saving... before something slow. Then you call the control's update() method, which sends it to the screen at once. Lesson F.5 does that.

The wrong way. update() only works on a control that's already on the page. Here it's called too early:

import flet as ft


def main(page: ft.Page) -> None:
    count = ft.Text("Nothing lent yet")
    count.value = "Lent today: 0"
    count.update()
    page.add(count)


ft.run(main)

Flet stops with RuntimeError: Text(9) Control must be added to the page first. Text(9) is the control: a Text, and the number Flet uses for it inside. That's fair: count is a Python object that's never been shown, so there's nothing on the screen to update.

The fix is to leave the update() out. Change fields as much as you like before page.add: when the control is added, it's drawn with whatever its fields are then. So update() is only for a control that's already showing, from code that isn't a handler, or before a handler has finished.

A handler changes label.value. When does the screen show it?

Light, dark and a theme

Flutter's Material Design builds a whole set of matching colours from one seed colour. Save this as theme.py and run it:

import flet as ft


def main(page: ft.Page) -> None:
    page.title = "Library desk"
    page.theme = ft.Theme(color_scheme_seed=ft.Colors.GREEN)
    page.theme_mode = ft.ThemeMode.LIGHT

    def flip(e: ft.Event[ft.Button]) -> None:
        dark = page.theme_mode == ft.ThemeMode.LIGHT
        page.theme_mode = ft.ThemeMode.DARK if dark else ft.ThemeMode.LIGHT

    page.add(ft.Text("Welcome to the library"), ft.Button(content="Light or dark", on_click=flip))


ft.run(main)
  • ft.Theme(color_scheme_seed=ft.Colors.GREEN) makes every control's colours from green: buttons, fields, highlights.
  • page.theme_mode is LIGHT, DARK, or SYSTEM to follow the computer's setting.
  • The button flips it. Changing a field of the page is like changing a field of a control: Flet redraws after the handler.

ft.Colors and ft.ThemeMode are enums: fixed sets of named values. Your editor can list them as you type ft.Colors., so you don't have to remember them.

Check

How do you put two buttons side by side under a heading?
Why does count.update() fail before page.add(count)?
What does color_scheme_seed do?

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.

All sources for Simple Python