How do you modify a PopMenuButton content when a popMenuItem is clicked in flet?

22 Views Asked by At

In my flet code I ask the user to select a format, and the I want to show in the popMenuButton content the item selected, but I actually don't have an idea of how, actually this doesn't crash but didn't work either.

tempType = ""

#METHODS ON CLICK
def onButtonClickNumber(e):
    tempType = "Número"
    print(tempType)
    popUpAddInput.update()


def onButtonClickBinary(e):
    tempType = "Binario"
    print(tempType)
    popUpAddInput.update()

def onButtonClickText(e):
    tempType = "Texto"
    print(tempType)
    popUpAddInput.update()

# SCREEN INPUT ADD ----------------------------------------------------------
popUpAddInput = ft.PopupMenuButton(
    
    content=ft.Container(
        ft.Row([
            ft.Icon(ft.icons.FORMAT_LIST_BULLETED_ROUNDED, color="BLACK",size=40),
            ft.Text(value="Selecciona el formato: "+ tempType,size = 16, color=ft.colors.BLACK, font_family = "Poppins", weight = ft.FontWeight.NORMAL),
        ])
    ),
    items=[
        ft.PopupMenuItem(
            content=ft.Row(
                [
                    ft.Icon(ft.icons.NUMBERS,color="BLACK"),
                    ft.Text("Número"),
                ]
            ),  
            on_click= onButtonClickNumber
        ),
        ft.PopupMenuItem(
            content=ft.Row(
                [
                    ft.Icon(ft.icons.FORMAT_COLOR_TEXT,color="BLACK"),
                    ft.Text("Texto"),
                ]
            ),
            on_click= onButtonClickText
        ),
        ft.PopupMenuItem(
            content=ft.Row(
                [
                    ft.Icon(ft.icons.RADIO_BUTTON_CHECKED_OUTLINED,color="BLACK"),
                    ft.Text("Binario"),
                ]
            ),
            on_click= onButtonClickBinary
        ),
    ]
)

In my flet code I ask the user to select a format, and the I want to show in the popMenuButton content the item selected, but I actually don't have an idea of how, actually this doesn't crash but didn't work either.

1

There are 1 best solutions below

0
r-beginners On BEST ANSWER

I understand that your question is to display the type selected in the pop-up menu in a string. You can simply create a separate text control for displaying the character type, assign a string to that control, and update that control.

import flet as ft

def main(page: ft.Page):
    tempType = ft.Text('')

    #METHODS ON CLICK
    def onButtonClickNumber(e):
        tempType.value = "Número"
        tempType.update()
        # popUpAddInput.update()

    def onButtonClickBinary(e):
        tempType.value = "Binario"
        tempType.update()
        # popUpAddInput.update()

    def onButtonClickText(e):
        tempType.value = "Texto"
        tempType.update()
        # popUpAddInput.update()

    # SCREEN INPUT ADD ----------------------------------------------------------
    popUpAddInput = ft.PopupMenuButton(
        content=ft.Container(
            ft.Row([
                ft.Icon(ft.icons.FORMAT_LIST_BULLETED_ROUNDED, color="BLACK",size=40),
                ft.Text(value="Selecciona el formato: ",
                        size=16,
                        color=ft.colors.BLACK,
                        font_family="Poppins",
                        weight=ft.FontWeight.NORMAL),
                tempType

            ])
        ),
        items=[
            ft.PopupMenuItem(
                content=ft.Row(
                    [
                        ft.Icon(ft.icons.NUMBERS,color="BLACK"),
                        ft.Text("Número"),
                    ]
                ),  
                on_click= onButtonClickNumber
            ),
            ft.PopupMenuItem(
                content=ft.Row(
                    [
                        ft.Icon(ft.icons.FORMAT_COLOR_TEXT,color="BLACK"),
                        ft.Text("Texto"),
                    ]
                ),
                on_click= onButtonClickText
            ),
            ft.PopupMenuItem(
                content=ft.Row(
                    [
                        ft.Icon(ft.icons.RADIO_BUTTON_CHECKED_OUTLINED,color="BLACK"),
                        ft.Text("Binario"),
                    ]
                ),
                on_click= onButtonClickBinary
            ),
        ]
    )
    print(popUpAddInput.content)

    page.add(popUpAddInput)

ft.app(target=main)