[Python] Python self.page.snack_bar doesn't work in Flet

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a beginner student and trying to validate my login code showing a snackbar mensage. There are no erros to run, but snackbar doesn't appear. At first a tried if a firebase authentication, and i think that could be wrong and tried with a simple condition, but doesn't work either, condition being right or not.

    flet --version 0.27.1

    what could be the problem? could someone help me? Thank You.

    import flet as ft
    from utilis.colors import *
    from database.firebase_config import auth_pyrebase


    class Login(ft.Container):
    def __init__(self, page: ft.Page):
    super().__init__()


    self.expand = True
    self.page = page

    self.user = ft.TextField(
    label= 'E-mail',
    border_color= BorderColor,
    width= 400,
    )

    self.password = ft.TextField(
    label= 'Password',
    password=True,
    can_reveal_password=True,
    border_color= BorderColor,
    width= 400,
    )

    self.btn_login = ft.ElevatedButton(
    text='Login',
    color= TxtColor,
    bgcolor=ButtonLoginColor,
    width=100,
    height= 40,
    on_click= self.login,
    )

    self.btn_singup = ft.ElevatedButton(
    text='Sign Up',
    color= TxtColor,
    bgcolor=ButtonSingupColor,
    width=100,
    height= 40,
    on_click= lambda e: page.go('/singup')
    )

    self.content = ft.Row(
    controls=[
    ft.Container(
    expand= 1,
    padding = ft.padding.all(20),
    content = ft.Column(
    alignment=ft.MainAxisAlignment.CENTER,
    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
    controls=[
    ft.Text('Login',
    color=TxtColor,
    size=40,
    weight=ft.FontWeight.BOLD),
    self.user,
    self.password,
    self.btn_login,
    self.btn_singup,
    ]
    )
    )
    ]
    )

    def login(self, e):

    # try:
    # auth_pyrebase.sign_in_with_email_and_password(
    # self.usuario.value, self.senha.value)
    if self.user.value == 'test' and self.password.value == 'test':
    self.page.snack_bar = ft.SnackBar(
    ft.Text(
    value='Login OK',
    color='white',
    ),
    bgcolor='green',
    action='OK',
    duration=3000,
    )
    self.page.snack_bar.open = True
    self.page.update()
    self.user.value = ""
    self.password.value = ""

    else:
    self.page.snack_bar = ft.SnackBar(
    ft.Text(
    value='Erro',
    color='white',
    ),
    bgcolor='red',
    action='OK',
    duration=3000,
    )
    self.page.snack_bar.open = True
    self.page.update()

    self.user.value = ""
    self.password.value = ""


    I already try to copy a example in Flet Docs, but also doesn't work.

    self.page.open(ft.SnackBar(
    ft.Text(
    value='Login OK!',
    bgcolor='green',
    action='OK',
    duration=3000,
    )))


    Minimal Reproducible Example

    import flet as ft

    class Login(ft.Container):
    def __init__(self, page: ft.Page):
    super().__init__()

    self.page = page

    self.user = ft.TextField(
    label= 'E-mail'
    )

    self.password = ft.TextField(
    label= 'Password',
    )

    self.btn_login = ft.ElevatedButton(
    text='Login',
    on_click= self.login,
    )

    self.content = ft.Row(
    controls=[
    ft.Container(
    content = ft.Column(
    controls=[
    ft.Text('Login'),
    self.user,
    self.password,
    self.btn_login,
    ]
    )
    )
    ]
    )

    def login(self, e):

    if self.user.value == 'test' and self.password.value == 'test':
    self.page.snack_bar = ft.SnackBar(
    ft.Text(
    value='Login OK',
    color='white',
    ),
    bgcolor='green',
    action='OK',
    )
    self.page.snack_bar.open = True
    self.page.update()
    else:
    self.page.snack_bar = ft.SnackBar(
    ft.Text(
    value='Erro',
    color='white',
    ),
    bgcolor='red',
    action='OK',
    )
    self.page.snack_bar.open = True
    self.page.update()

    Continue reading...

Compartilhe esta Página