Skip to content

Commit 3c8f567

Browse files
committed
Bindings and focus example Counter
1 parent 86a6fcc commit 3c8f567

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

docs/examples/guide/widgets/counter.tcss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Counter {
33
padding: 1 2;
44
color: $text-muted;
55

6-
&:focus {
6+
&:focus { /* (1)! */
77
background: $primary;
88
color: $text;
99
text-style: bold;

docs/examples/guide/widgets/counter01.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from textual.widgets import Footer, Static
44

55

6-
class Counter(Static, can_focus=True):
6+
class Counter(Static, can_focus=True): # (1)!
77
"""A counter that can be incremented and decremented by pressing keys."""
88

99
count = reactive(0)

docs/guide/widgets.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,41 @@ A widget is only be able to handle key presses if it or one of its descendants h
200200
To demonstrate, let's design a simple interactive counter widget which can be incremented and decremented using the keyboard.
201201

202202
In the following example, we define a simple `Counter` widget with `can_focus=True`, and some CSS to make it stand out when focused.
203+
Our app contains three `Counter` widgets, which we can move focus between using ++tab++ and ++shift+tab++.
203204

204205
=== "counter01.py"
205206

206207
```python title="counter01.py" hl_lines="6"
207208
--8<-- "docs/examples/guide/widgets/counter01.py"
208209
```
209210

211+
1. Allow the widget to receive input focus.
212+
210213
=== "counter.tcss"
211214

212215
```css title="counter.tcss" hl_lines="6-11"
213216
--8<-- "docs/examples/guide/widgets/counter.tcss"
214217
```
215218

219+
1. These styles are applied only when the widget has focus.
220+
216221
=== "Output"
217222

218223
```{.textual path="docs/examples/guide/widgets/counter01.py"}
219224
```
220225

221226
Notice that Textual automatically focused the first widget, and that pressing ++tab++ and ++shift+tab++ will move focus between widgets.
222227

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.
228+
!!! note
229+
230+
You can also move focus to a widget by clicking on it.
231+
232+
Now that our counter is focusable, let's make it interactive by adding some key bindings and actions to it.
233+
To do this, we add a `BINDINGS` class variable to `Counter`, with bindings for ++up++ and ++down++.
225234
These new bindings are linked to the `change_count` action, which updates the `count` reactive attribute.
226235

236+
With our bindings in place, we can now change the count of the _currently focused counter_ using ++up++ and ++down++.
237+
227238
=== "counter02.py"
228239

229240
```python title="counter02.py" hl_lines="9-12 19-20"
@@ -238,12 +249,9 @@ These new bindings are linked to the `change_count` action, which updates the `c
238249

239250
=== "Output"
240251

241-
```{.textual path="docs/examples/guide/widgets/counter02.py"}
252+
```{.textual path="docs/examples/guide/widgets/counter02.py" press="up,tab,down,down"}
242253
```
243254

244-
With our bindings in place, we can now change the count of the _currently focused counter_ using ++up++ and ++down++.
245-
246-
247255
## Rich renderables
248256

249257
In previous examples we've set strings as content for Widgets. You can also use special objects called [renderables](https://rich.readthedocs.io/en/latest/protocol.html) for advanced visuals. You can use any renderable defined in [Rich](https://github.com/Textualize/rich) or third party libraries.

0 commit comments

Comments
 (0)