2022-08-22 11:26:39 +01:00
from textual . app import App , ComposeResult
2024-11-18 22:01:44 +00:00
from textual . containers import HorizontalGroup , VerticalScroll
from textual . widgets import Button , Digits , Footer , Header
2022-08-20 20:29:19 +01:00
2024-11-18 22:01:44 +00:00
class TimeDisplay ( Digits ) :
2022-08-22 11:26:39 +01:00
""" A widget to display elapsed time. """
2022-08-20 20:29:19 +01:00
2024-11-18 22:01:44 +00:00
class Stopwatch ( HorizontalGroup ) :
2022-08-22 11:26:39 +01:00
""" A stopwatch widget. """
def on_button_pressed ( self , event : Button . Pressed ) - > None :
""" Event handler called when a button is pressed. """
2022-08-21 09:47:42 +01:00
if event . button . id == " start " :
2022-08-20 20:29:19 +01:00
self . add_class ( " started " )
2022-08-21 09:47:42 +01:00
elif event . button . id == " stop " :
2022-08-20 20:29:19 +01:00
self . remove_class ( " started " )
2022-08-22 11:26:39 +01:00
def compose ( self ) - > ComposeResult :
""" Create child widgets of a stopwatch. """
2022-08-20 20:29:19 +01:00
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 ) :
2022-08-22 11:26:39 +01:00
""" A Textual app to manage stopwatches. """
2023-08-22 11:48:17 +01:00
CSS_PATH = " stopwatch04.tcss "
2022-09-09 11:38:26 +01:00
BINDINGS = [ ( " d " , " toggle_dark " , " Toggle dark mode " ) ]
2022-08-22 11:26:39 +01:00
def compose ( self ) - > ComposeResult :
""" Create child widgets for the app. """
2022-08-20 20:29:19 +01:00
yield Header ( )
yield Footer ( )
2024-11-18 22:01:44 +00:00
yield VerticalScroll ( Stopwatch ( ) , Stopwatch ( ) , Stopwatch ( ) )
2022-08-20 20:29:19 +01:00
2022-08-22 11:26:39 +01:00
def action_toggle_dark ( self ) - > None :
""" An action to toggle dark mode. """
2024-10-24 16:54:54 +01:00
self . theme = (
" textual-dark " if self . theme == " textual-light " else " textual-light "
)
2022-08-20 20:29:19 +01:00
if __name__ == " __main__ " :
2022-09-18 22:02:08 +01:00
app = StopwatchApp ( )
2022-08-20 20:29:19 +01:00
app . run ( )