How to get a value from a variable in my class?

  Kiến thức lập trình

I’m creating a simple stopwatch on flet (I don’t think it matters) I have a block of code from which I would later like to extract a variable, how do I do this?

import asyncio
import flet as ft

class Stopwatch(ft.Text):
    def __init__(self, seconds):
        super().__init__()
        self.seconds = seconds #here is the variable

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

    def will_unmount(self):
        self.running = False

    async def update_timer(self):
        while self.seconds >= 0 and self.running:
            mins, secs = divmod(self.seconds, 60)
            self.value = "{:02d}:{:02d}".format(mins, secs)
            self.size = 25
            self.weight=ft.FontWeight.W_600
            self.update()
            await asyncio.sleep(1)
            self.seconds += 1 #here is the variable


print(Stopwatch.seconds) #how to get the value?

2

I’m not familiar with flet, but you could create an instance of your class:

 sw = Stopwatch(30)

And then, because it initialized with your __init__() code, you can access the variables in that instance:

 print(sw.seconds)

Whether that works in flet’s development pattern, I’m not sure.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT