mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
fix for removing
This commit is contained in:
@@ -5,7 +5,6 @@ TimerWidget {
|
||||
border: tall $panel-darken-2;
|
||||
margin: 1;
|
||||
padding: 0 1;
|
||||
|
||||
transition: background 200ms linear;
|
||||
}
|
||||
|
||||
@@ -23,6 +22,7 @@ Button {
|
||||
dock: left;
|
||||
}
|
||||
|
||||
|
||||
TimerWidget.started {
|
||||
opacity: 100%;
|
||||
text-style: bold;
|
||||
@@ -43,13 +43,11 @@ TimerWidget.started #reset {
|
||||
visibility: hidden
|
||||
}
|
||||
|
||||
|
||||
#stop {
|
||||
dock: left;
|
||||
display: none;
|
||||
}
|
||||
|
||||
|
||||
Button#reset {
|
||||
#reset {
|
||||
dock: right;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from time import time
|
||||
from time import monotonic
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.layout import Container
|
||||
@@ -14,7 +14,7 @@ class TimeDisplay(Static):
|
||||
def watch_time_delta(self, time_delta: float) -> None:
|
||||
minutes, seconds = divmod(time_delta, 60)
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
self.update(f"{hours:02.0f}:{minutes:02.0f}:{seconds:02.2f}")
|
||||
self.update(f"{hours:02.0f}:{minutes:02.0f}:{seconds:05.2f}")
|
||||
|
||||
|
||||
class TimerWidget(Static):
|
||||
@@ -28,10 +28,9 @@ class TimerWidget(Static):
|
||||
self.update_timer = self.set_interval(1 / 30, self.update_elapsed, pause=True)
|
||||
|
||||
def update_elapsed(self) -> None:
|
||||
time_delta = (
|
||||
self.total + time() - self.start_time if self.started else self.total
|
||||
self.query_one(TimeDisplay).time_delta = (
|
||||
self.total + monotonic() - self.start_time if self.started else self.total
|
||||
)
|
||||
self.query_one(TimeDisplay).time_delta = time_delta
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Button("Start", id="start", variant="success")
|
||||
@@ -41,13 +40,15 @@ class TimerWidget(Static):
|
||||
|
||||
def watch_started(self, started: bool) -> None:
|
||||
if started:
|
||||
self.start_time = time()
|
||||
self.start_time = monotonic()
|
||||
self.update_timer.resume()
|
||||
self.add_class("started")
|
||||
self.query_one("#stop").focus()
|
||||
else:
|
||||
self.update_timer.pause()
|
||||
self.total += time() - self.start_time
|
||||
self.total += monotonic() - self.start_time
|
||||
self.remove_class("started")
|
||||
self.query_one("#start").focus()
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
button_id = event.button.id
|
||||
@@ -59,6 +60,8 @@ class TimerWidget(Static):
|
||||
|
||||
|
||||
class TimerApp(App):
|
||||
"""Manage the timers."""
|
||||
|
||||
def on_load(self) -> None:
|
||||
self.bind("a", "add_timer", description="Add")
|
||||
self.bind("r", "remove_timer", description="Remove")
|
||||
@@ -70,8 +73,8 @@ class TimerApp(App):
|
||||
|
||||
def action_add_timer(self) -> None:
|
||||
new_timer = TimerWidget()
|
||||
self.query_one("Container").mount(new_timer)
|
||||
self.call_later(new_timer.scroll_visible)
|
||||
self.query_one(Container).mount(new_timer)
|
||||
new_timer.scroll_visible()
|
||||
|
||||
def action_remove_timer(self) -> None:
|
||||
timers = self.query("Container TimerWidget")
|
||||
@@ -79,6 +82,6 @@ class TimerApp(App):
|
||||
timers.last().remove()
|
||||
|
||||
|
||||
app = TimerApp(css_path="timers.css")
|
||||
app = TimerApp(title="TimerApp", css_path="timers.css")
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user