[Python] How to build a generic user control in Python Flet

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to develop a custom user control in Flet (version==0.28.2). The page contains a button_click event that implements the control:

    def button_on_click(e):
    page_content.controls=[Create_New_Db_UI()]
    page.update()


    The user control is defined by the class:

    class Create_New_Db_UI(ft.Control):
    def __init__(self, **kwargs):
    super().__init__(**kwargs)

    def _get_control_name(self):
    return "Create_New_Db_UI"

    def build(self):
    return ft.Column(
    [
    ft.Text("Create New Database", size=20, weight=ft.FontWeight.BOLD),
    ft.Divider(),
    ft.Text("Test", size=16,),
    ],
    )


    After running the code, the UI contains a red rectangle with the message: "Unknown control: Create_New_Db_UI", so the control is not shown. In a previous version of Flet (version==0.21.2), I used successfully the class definition as:

    class Create_New_Db_Ui(ft.UserControl)


    which is unavailable in this version. Of course, I can write

    class Create_New_Db_UI(ft.Column):
    def __init__(self, **kwargs):
    super().__init__(**kwargs)

    def build(self):
    self.controls =[
    ft.Text("Create New Database", size=20, weight=ft.FontWeight.BOLD),
    ft.Divider(),
    ft.Text("Test", size=16,),
    ]

    return self


    But how can I define a new generic user custom control? Which is the correct class from which my control must inherit?

    Continue reading...

Compartilhe esta Página