Skip to content

Commit ed5864d

Browse files
committed
Add notes to code
1 parent 6382685 commit ed5864d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

docs/examples/guide/widgets/counter02.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Counter(Static, can_focus=True):
77
"""A counter that can be incremented and decremented by pressing keys."""
88

99
BINDINGS = [
10-
("up,k", "change_count(1)", "Increment"),
10+
("up,k", "change_count(1)", "Increment"), # (1)!
1111
("down,j", "change_count(-1)", "Decrement"),
1212
]
1313

@@ -16,7 +16,7 @@ class Counter(Static, can_focus=True):
1616
def render(self) -> RenderResult:
1717
return f"Count: {self.count}"
1818

19-
def action_change_count(self, amount: int):
19+
def action_change_count(self, amount: int) -> None: # (2)!
2020
self.count += amount
2121

2222

docs/guide/widgets.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ If the supplied text is too long to fit within the widget, it will be cropped (a
190190
There are a number of styles that influence how titles are displayed (color and alignment).
191191
See the [style reference](../styles/index.md) for details.
192192

193-
## Interacting with widgets
193+
## Focus & keybindings
194194

195195
Widgets can have a list of associated key [bindings](../guide/input.md#bindings),
196196
which let them call [actions](../guide/actions.md) in response to key presses.
@@ -227,14 +227,17 @@ Now that our counter is focusable, let's make it interactive by adding some key
227227
To do this, we add a `BINDINGS` class variable to `Counter`, with bindings for ++up++ and ++down++.
228228
These new bindings are linked to the `change_count` action, which updates the `count` reactive attribute.
229229

230-
With our bindings in place, we can now change the count of the _currently focused counter_ using ++up++ and ++down++.
230+
With our bindings in place, we can now change the count of the _currently focused_ counter using ++up++ and ++down++.
231231

232232
=== "counter02.py"
233233

234234
```python title="counter02.py" hl_lines="9-12 19-20"
235235
--8<-- "docs/examples/guide/widgets/counter02.py"
236236
```
237237

238+
1. Associates presses of ++up++ or ++k++ with the `change_count` action, passing `1` as the argument to increment the count. The final argument ("Increment") is a user-facing label displayed in the footer when this binding is active.
239+
2. Called when the binding is triggered. Take care to add the `action_` prefix to the method name.
240+
238241
=== "counter.tcss"
239242

240243
```css title="counter.tcss"

0 commit comments

Comments
 (0)