You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
-
67
54
## Static widget
68
55
69
56
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
203
190
There are a number of styles that influence how titles are displayed (color and alignment).
204
191
See the [style reference](../styles/index.md) for details.
205
192
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:
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
+
206
228
## Rich renderables
207
229
208
230
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