[Python] Flet update icon in async

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I have problem with flet updating icon, self.value is updating perfect but why icon does not changing at the start of class it adds icon ft.icons.SIGNAL_WIFI_STATUSBAR_NULL_OUTLINED but in async it does not updating in interface

    import asyncio
    import flet as ft
    import requests
    import time

    class Countdown(ft.Text):
    def __init__(self):
    super().__init__()
    self.running = False
    self.value = "Connecting..."
    self.icon = ft.Icon(ft.icons.SIGNAL_WIFI_STATUSBAR_NULL_OUTLINED)

    def did_mount(self):
    self.running = True
    self.update()
    self.page.run_task(self.update_timer)

    def will_unmount(self):
    self.running = False

    async def update_timer(self):
    while self.running:
    try:
    requests.get("http://www.google.com", timeout=3)
    self.value = "Connected"
    self.icon = ft.Icon(ft.icons.WIFI)
    self.chiconke()
    self.update()
    print(self.icon)
    except requests.ConnectionError:
    self.value = "Disconnected"
    self.icon = ft.icons.WIFI
    self.update()
    await asyncio.sleep(5)

    def create_statusbar():
    countdown = Countdown()

    return ft.Container(
    content=ft.Row(
    [
    ft.CupertinoListTile(
    bgcolor_activated=ft.colors.AMBER_ACCENT,
    leading=countdown.icon,
    title=countdown,
    width=180,
    ),

    ft.CupertinoListTile( # Example: API status
    bgcolor_activated=ft.colors.AMBER_ACCENT,
    leading=ft.Icon(name=ft.icons.API),
    title=ft.Text(countdown.icon),
    subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
    width=150,
    ),

    ft.CupertinoListTile( # Example: API status
    bgcolor_activated=ft.colors.AMBER_ACCENT,
    leading=ft.Icon(name=ft.icons.API),
    title=ft.Text("API Status"),
    subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
    width=150,
    ),

    ft.CupertinoListTile( # Example: API status
    bgcolor_activated=ft.colors.AMBER_ACCENT,
    leading=ft.Icon(name=ft.icons.API),
    title=ft.Text("API Status"),
    subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
    width=150,
    ),
    ],
    ),
    border=ft.border.all(1, ft.colors.GREY_700),
    border_radius=ft.border_radius.all(10),
    )


    I have tried everything, async updates text but not icon.

    Continue reading...

Compartilhe esta Página