Flet is a Python library that allows developers to build real-time web, desktop, and mobile apps using Python, while Flutter handles the UI internally. In this article, we will learn how to create Flutter-like applications using the Flet library with only Python.
To start building apps with Flet, install the library using the following command in your terminal:
pip install flet
Below is an example that shows how a Flet application starts and runs.
Output
A blank app window opens with a default theme and a loading animation, because no UI elements are added yet.
myapp() is the main function where UI components will be added.
page represents the app window or screen.
flt.app(target=myapp) starts the Flet application using the given function.
Changing App Theme in Flet
Flet allows you to easily switch the app theme between Light and Dark mode. By default, Flet follows your deviceβs system theme. You can override this behavior by explicitly setting the theme in code.
Output
π Image App opens with the selected theme applied
Explanation:
page.theme_mode controls the app theme.
ThemeMode.DARK forces dark mode (use ThemeMode.LIGHT for light mode).
page.window_height and page.window_width define the app window size.
page.update() applies all changes to the UI.
Adding Text to Your Flet App
Now letβs add some text elements to the app. In Flet, UI elements (called widgets) are added to the page using the page.add() method.
Output
π Image A text field with a label and default text appears on the page.
Explanation:
TextField() creates an input box.
label appears at the top of the text field.
value is the default text shown inside the field.
page.add(text) displays the TextField on the app screen.
Important note
When you use page.add(), Flet automatically updates the UI.
If all changes are done using add(), calling page.update() is optional.
For changes not added via add() (like page.title), itβs safer to call page.update().
Using both add() and update() together is allowed and causes no errors.
Changing TextField Border Style
By default, a TextField in Flet uses an outline border. You can easily customize its appearance by changing the border property to remove the border or display only an underline.
Higher numbers in color shades (e.g., YELLOW_300) make the color appear deeper.
weight=FontWeight.BOLD makes the text bold.
Calculator using Flet
In this section, we will build a simple yet functional calculator application using the Flet library. This example demonstrates how common UI components like buttons, text, rows, and columns can be combined to create an interactive Flutter-like app using only Python.