diff --git a/docs/examples/widgets/digit_display_reacting.py b/docs/examples/widgets/digit_display_reacting.py new file mode 100644 index 00000000000..46318a1e134 --- /dev/null +++ b/docs/examples/widgets/digit_display_reacting.py @@ -0,0 +1,20 @@ +from textual.app import App, ComposeResult +from textual.widgets import Input +from textual.widgets import DigitDisplay + + +class MyApp(App): + BINDINGS = [] + + def compose(self) -> ComposeResult: + yield Input(placeholder="Type something:") + yield DigitDisplay("") + + def on_input_changed(self, event: Input.Changed) -> None: + display: DigitDisplay = self.query_one(DigitDisplay) + display.digits = "".join(d for d in event.value if d in DigitDisplay.supported_digits) + + +if __name__ == "__main__": + app = MyApp() + app.run() diff --git a/docs/widget_gallery.md b/docs/widget_gallery.md index 18d2fd00d3f..1451c5d3cae 100644 --- a/docs/widget_gallery.md +++ b/docs/widget_gallery.md @@ -52,6 +52,15 @@ A powerful data table, with configurable cursors. ```{.textual path="docs/examples/widgets/data_table.py"} ``` +## DigitDisplay + +A widget to display digits and basic arithmetic operators using Unicode blocks. + +[DigitDisplay reference](./widgets/digit_display.md){ .md-button .md-button--primary } + +```{.textual path="docs/examples/widgets/digit_display.py"} +``` + ## DirectoryTree A tree view of files and folders. diff --git a/docs/widgets/digit_display.md b/docs/widgets/digit_display.md new file mode 100644 index 00000000000..e451aee1465 --- /dev/null +++ b/docs/widgets/digit_display.md @@ -0,0 +1,65 @@ +# DigitDisplay + +A widget to display digits and basic arithmetic operators using Unicode blocks. + +- [ ] Focusable +- [ ] Container + +## Examples + +=== "Static example" + + ```{.textual path="docs/examples/widgets/digit_display.py"} + ``` + +=== "digit_display.py" + + ```python + --8<-- "docs/examples/widgets/digit_display.py" + ``` + +=== "Reacting to an input" + + ```{.textual path="docs/examples/widgets/digit_display_reacting.py"} + ``` + +=== "digit_display_reacting.py" + + ```python + --8<-- "docs/examples/widgets/digit_display_reacting.py" + ``` + +## Reactive attributes + +| Name | Type | Default | Description | +| ------ | ------ | ------- | ---------------------------------------------- | +| `digits` | `str` | `""` | Use this to update the digits to be displayed. | + + +## Read-only attributes + +| Name | Type | Description | +| ------ | ------ | ----------------------------------------- | +| `supported_digits` | `frozenset[str]` | Contains the list of supported digits/characters. + +## Messages + +This widget sends no messages. + + +## Bindings + +This widget defines no bindings. + + +## Component classes + +This widget provides no component classes. + + +--- + + +::: textual.widgets.DigitDisplay + options: + heading_level: 2 diff --git a/src/textual/widgets/_digit_display.py b/src/textual/widgets/_digit_display.py index cb8aaa37473..fdbb8d9215d 100644 --- a/src/textual/widgets/_digit_display.py +++ b/src/textual/widgets/_digit_display.py @@ -197,11 +197,11 @@ } -class SingleDigitDisplay(Static): +class _SingleDigitDisplay(Static): digit = reactive(" ", layout=True) DEFAULT_CSS = """ - SingleDigitDisplay { + _SingleDigitDisplay { height: 3; min-width: 2; max-width: 3; @@ -224,6 +224,8 @@ class DigitDisplay(Widget): digits = reactive("", layout=True) + supported_digits = frozenset(_character_map.keys()) + DEFAULT_CSS = """ DigitDisplay { layout: horizontal; @@ -233,7 +235,7 @@ class DigitDisplay(Widget): def __init__(self, initial_value="", **kwargs): super().__init__(**kwargs) - self._displays = [SingleDigitDisplay(d) for d in initial_value] + self._displays = [_SingleDigitDisplay(d) for d in initial_value] self.digits = initial_value def compose(self) -> ComposeResult: @@ -241,7 +243,7 @@ def compose(self) -> ComposeResult: yield widget def _add_digit_widget(self, digit: str) -> None: - new_widget = SingleDigitDisplay(digit) + new_widget = _SingleDigitDisplay(digit) self._displays.append(new_widget) self.mount(new_widget)