from textual.app import App, ComposeResult from textual.containers import ScrollableContainer from textual.widgets import Button, Footer, Header, Static class TimeDisplay(Static): """A widget to display elapsed time.""" class Stopwatch(Static): """A stopwatch widget.""" def on_button_pressed(self, event: Button.Pressed) -> None: """Event handler called when a button is pressed.""" if event.button.id == "start": self.add_class("started") elif event.button.id == "stop": self.remove_class("started") def compose(self) -> ComposeResult: """Create child widgets of a stopwatch.""" yield Button("Start", id="start", variant="success") yield Button("Stop", id="stop", variant="error") yield Button("Reset", id="reset") yield TimeDisplay("00:00:00.00") class StopwatchApp(App): """A Textual app to manage stopwatches.""" CSS_PATH = "stopwatch04.css" BINDINGS = [("d", "toggle_dark", "Toggle dark mode")] def compose(self) -> ComposeResult: """Create child widgets for the app.""" yield Header() yield Footer() yield ScrollableContainer(Stopwatch(), Stopwatch(), Stopwatch()) def action_toggle_dark(self) -> None: """An action to toggle dark mode.""" self.dark = not self.dark if __name__ == "__main__": app = StopwatchApp() app.run()