Skip to content

Commit 86a6fcc

Browse files
committed
Counter example
1 parent 176cd5e commit 86a6fcc

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

docs/examples/guide/widgets/counter01.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CounterApp(App[None]):
1616
CSS_PATH = "counter.tcss"
1717

1818
def compose(self) -> ComposeResult:
19+
yield Counter()
1920
yield Counter()
2021
yield Counter()
2122
yield Footer()

docs/examples/guide/widgets/counter02.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CounterApp(App[None]):
2424
CSS_PATH = "counter.tcss"
2525

2626
def compose(self) -> ComposeResult:
27+
yield Counter()
2728
yield Counter()
2829
yield Counter()
2930
yield Footer()

docs/guide/widgets.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ which let them call [actions](../guide/actions.md) in response to key presses.
197197

198198
A widget is only be able to handle key presses if it or one of its descendants has [focus](./guide/input.md#input-focus).
199199

200-
Let's design a simple interactive `Counter` widget.
201-
We'll set `can_focus=True` to allow the widget to receive focus, and give it some simple to highlight it when it has focus:
200+
To demonstrate, let's design a simple interactive counter widget which can be incremented and decremented using the keyboard.
201+
202+
In the following example, we define a simple `Counter` widget with `can_focus=True`, and some CSS to make it stand out when focused.
202203

203204
=== "counter01.py"
204205

@@ -208,7 +209,7 @@ We'll set `can_focus=True` to allow the widget to receive focus, and give it som
208209

209210
=== "counter.tcss"
210211

211-
```css title="counter.tcss"
212+
```css title="counter.tcss" hl_lines="6-11"
212213
--8<-- "docs/examples/guide/widgets/counter.tcss"
213214
```
214215

@@ -217,12 +218,30 @@ We'll set `can_focus=True` to allow the widget to receive focus, and give it som
217218
```{.textual path="docs/examples/guide/widgets/counter01.py"}
218219
```
219220

220-
Textual will automatically focus the first counter, and you'll notice that it's been highlighted with a blue background thanks to the CSS we applied using the `:focus` pseudo-selector! ++tab++ and ++shift+tab++ moves focus between the two counters.
221+
Notice that Textual automatically focused the first widget, and that pressing ++tab++ and ++shift+tab++ will move focus between widgets.
222+
223+
Now that our counter is focusable, let's add some keybindings for incrementing and decrementing the counter.
224+
To do this, we add a `BINDINGS` class variable to `Counter`, with bindings for incrementing and decrementing the counter using ++up++ and ++down++ respectively.
225+
These new bindings are linked to the `change_count` action, which updates the `count` reactive attribute.
226+
227+
=== "counter02.py"
228+
229+
```python title="counter02.py" hl_lines="9-12 19-20"
230+
--8<-- "docs/examples/guide/widgets/counter02.py"
231+
```
232+
233+
=== "counter.tcss"
221234

222-
Now that we have a focusable widget, let's add some key bindings to it for incrementing and decrementing the counter.
223-
To do this, we'll add a `BINDINGS` class variable to the `Counter`.
235+
```css title="counter.tcss"
236+
--8<-- "docs/examples/guide/widgets/counter.tcss"
237+
```
224238

239+
=== "Output"
240+
241+
```{.textual path="docs/examples/guide/widgets/counter02.py"}
242+
```
225243

244+
With our bindings in place, we can now change the count of the _currently focused counter_ using ++up++ and ++down++.
226245

227246

228247
## Rich renderables

0 commit comments

Comments
 (0)