[Python] Dynamically created buttons and on_click event flet

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

  1. Stack

    Stack Membro Participativo

    What is the problem with this code? The last button that is created is always the one that is clicked!

    def main(page: ft.Page):

    def click(e, button: ft. Button):
    button.bgcolor = ft.Colors.YELLOW
    button.page.update()

    words = ['ma', 'ti', 'ke', 'to', 'pe', 'la', 'su']
    buttons = []

    for word in words:
    button = ft.Button(text=word)
    button.on_click = lambda e: click(e, button)
    buttons.append(button)

    page.add(ft.Row(buttons))

    ft.app(main)

    Continue reading...

Compartilhe esta Página