Python self.page.snack_bar funktioniert nicht in flet
Posted: 23 Feb 2025, 06:51
Ich bin ein Anfängerstudent und versuche, meinen Login -Code zu validieren, der eine Snackbarmensage zeigt. Es gibt keine Fehler zu laufen, aber Snackbars erscheint nicht.
Zuerst ein Versuch, wenn eine Firebase -Authentifizierung Rechte oder nicht. Könnte mir jemand helfen? Danke. < /P>
Zuerst ein Versuch, wenn eine Firebase -Authentifizierung Rechte oder nicht. Könnte mir jemand helfen? Danke. < /P>
Code: Select all
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 = ""
< /code>
Ich versuche bereits, ein Beispiel in Flet -Dokumenten zu kopieren, funktioniert aber auch nicht. < /p>
self.page.open(ft.SnackBar(
ft.Text(
value='Login OK!',
bgcolor='green',
action='OK',
duration=3000,
)))
< /code>
minimal reproduzierbares Beispiel < /p>
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()