mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
stopwatch04
This commit is contained in:
@@ -25,23 +25,6 @@ class Stopwatch(Static):
|
||||
total = Reactive(0.0)
|
||||
started = Reactive(False)
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Called when widget is first added."""
|
||||
self.update_timer = self.set_interval(1 / 30, self.update_elapsed, pause=True)
|
||||
|
||||
def update_elapsed(self) -> None:
|
||||
"""Updates elapsed time."""
|
||||
self.query_one(TimeDisplay).time = (
|
||||
self.total + monotonic() - self.start_time if self.started else self.total
|
||||
)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Composes the timer widget."""
|
||||
yield Button("Start", id="start", variant="success")
|
||||
yield Button("Stop", id="stop", variant="error")
|
||||
yield TimeDisplay()
|
||||
yield Button("Reset", id="reset")
|
||||
|
||||
def watch_started(self, started: bool) -> None:
|
||||
"""Called when the 'started' attribute changes."""
|
||||
if started:
|
||||
@@ -63,6 +46,23 @@ class Stopwatch(Static):
|
||||
self.total = 0.0
|
||||
self.update_elapsed()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Called when widget is first added."""
|
||||
self.update_timer = self.set_interval(1 / 30, self.update_elapsed, pause=True)
|
||||
|
||||
def update_elapsed(self) -> None:
|
||||
"""Updates elapsed time."""
|
||||
self.query_one(TimeDisplay).time = (
|
||||
self.total + monotonic() - self.start_time if self.started else self.total
|
||||
)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Composes the timer widget."""
|
||||
yield Button("Start", id="start", variant="success")
|
||||
yield Button("Stop", id="stop", variant="error")
|
||||
yield Button("Reset", id="reset")
|
||||
yield TimeDisplay()
|
||||
|
||||
|
||||
class StopwatchApp(App):
|
||||
"""Manage the timers."""
|
||||
@@ -87,7 +87,7 @@ class StopwatchApp(App):
|
||||
|
||||
def action_remove_timer(self) -> None:
|
||||
"""Called to remove a timer."""
|
||||
timers = self.query("#timers TimerWidget")
|
||||
timers = self.query("#timers Stopwatch")
|
||||
if timers:
|
||||
timers.last().remove()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user