Skip to content

Commit 176cd5e

Browse files
committed
More docs
1 parent 715080c commit 176cd5e

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from textual.app import App, ComposeResult
2+
from textual.reactive import reactive
3+
from textual.widgets import Footer, Static
4+
5+
6+
class Counter(Static, can_focus=True):
7+
"""A counter that can be incremented and decremented by pressing keys."""
8+
9+
count = reactive(0)
10+
11+
def render(self) -> str:
12+
return f"Count: {self.count}"
13+
14+
15+
class CounterApp(App[None]):
16+
CSS_PATH = "counter.tcss"
17+
18+
def compose(self) -> ComposeResult:
19+
yield Counter()
20+
yield Counter()
21+
yield Footer()
22+
23+
24+
if __name__ == "__main__":
25+
app = CounterApp()
26+
app.run()

docs/examples/guide/widgets/counter.py renamed to docs/examples/guide/widgets/counter02.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from textual.app import App, ComposeResult
2-
from textual.binding import Binding
32
from textual.reactive import reactive
43
from textual.widgets import Footer, Static
54

@@ -8,16 +7,16 @@ class Counter(Static, can_focus=True):
87
"""A counter that can be incremented and decremented by pressing keys."""
98

109
BINDINGS = [
11-
Binding("up,k", "change_count(1)", "Increment"),
12-
Binding("down,j", "change_count(-1)", "Decrement"),
10+
("up,k", "change_count(1)", "Increment"),
11+
("down,j", "change_count(-1)", "Decrement"),
1312
]
1413

1514
count = reactive(0)
1615

1716
def render(self) -> str:
1817
return f"Count: {self.count}"
1918

20-
def action_change_count(self, amount: int) -> None:
19+
def action_change_count(self, amount: int):
2120
self.count += amount
2221

2322

docs/guide/widgets.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ The addition of the CSS has completely transformed our custom widget.
5151
```{.textual path="docs/examples/guide/widgets/hello02.py"}
5252
```
5353

54-
### Responding to key presses
55-
56-
Widgets can have a list of associated key [bindings](../guide/input.md#bindings),
57-
which enable a it to call [actions](../guide/actions.md) in response to key presses.
58-
59-
A widget's bindings will only be checked if it or one of its descendants has focus.
60-
61-
Let's look at Textual's builtin [Button](../widgets/button.md) widget to see an example of how widget bindings work.
62-
The `Button` widget has a single binding for the `enter` key.
63-
When a button is focused, and the user presses ++enter++, the `action_press` method inside `Button` is called.
64-
65-
66-
6754
## Static widget
6855

6956
While you can extend the Widget class, a subclass will typically be a better starting point. The [Static][textual.widgets.Static] class is a widget subclass which caches the result of render, and provides an [update()][textual.widgets.Static.update] method to update the content area.
@@ -203,6 +190,41 @@ If the supplied text is too long to fit within the widget, it will be cropped (a
203190
There are a number of styles that influence how titles are displayed (color and alignment).
204191
See the [style reference](../styles/index.md) for details.
205192

193+
## Interacting with widgets
194+
195+
Widgets can have a list of associated key [bindings](../guide/input.md#bindings),
196+
which let them call [actions](../guide/actions.md) in response to key presses.
197+
198+
A widget is only be able to handle key presses if it or one of its descendants has [focus](./guide/input.md#input-focus).
199+
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:
202+
203+
=== "counter01.py"
204+
205+
```python title="counter01.py" hl_lines="6"
206+
--8<-- "docs/examples/guide/widgets/counter01.py"
207+
```
208+
209+
=== "counter.tcss"
210+
211+
```css title="counter.tcss"
212+
--8<-- "docs/examples/guide/widgets/counter.tcss"
213+
```
214+
215+
=== "Output"
216+
217+
```{.textual path="docs/examples/guide/widgets/counter01.py"}
218+
```
219+
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+
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`.
224+
225+
226+
227+
206228
## Rich renderables
207229

208230
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)