mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
handler name in classvar (#2675)
* handler name in classvar * fix for worker handler name * fix custom templates, event docs * doc tweak * doc tweak * restore signature
This commit is contained in:
@@ -148,13 +148,40 @@ Most of the logic in a Textual app will be written in message handlers. Let's ex
|
||||
Textual uses the following scheme to map messages classes on to a Python method.
|
||||
|
||||
- Start with `"on_"`.
|
||||
- Add the messages' namespace (if any) converted from CamelCase to snake_case plus an underscore `"_"`
|
||||
- Add the message's namespace (if any) converted from CamelCase to snake_case plus an underscore `"_"`.
|
||||
- Add the name of the class converted from CamelCase to snake_case.
|
||||
|
||||
<div class="excalidraw">
|
||||
--8<-- "docs/images/events/naming.excalidraw.svg"
|
||||
</div>
|
||||
|
||||
Messages have a namespace if they are defined as a child class of a Widget.
|
||||
The namespace is the name of the parent class.
|
||||
For instance, the builtin `Input` class defines it's `Changed` message as follow:
|
||||
|
||||
```python
|
||||
class Input(Widget):
|
||||
...
|
||||
class Changed(Message):
|
||||
"""Posted when the value changes."""
|
||||
...
|
||||
```
|
||||
|
||||
Because `Changed` is a *child* class of `Input`, its namespace will be "input" (and the handler name will be `on_input_changed`).
|
||||
This allows you to have similarly named events, without clashing event handler names.
|
||||
|
||||
!!! tip
|
||||
|
||||
If you are ever in doubt about what the handler name should be for a given event, print the `handler_name` class variable for your event class.
|
||||
|
||||
Here's how you would check the handler name for the `Input.Changed` event:
|
||||
|
||||
```py
|
||||
>>> from textual.widgets import Input
|
||||
>>> Input.Changed.handler_name
|
||||
'on_input_changed'
|
||||
```
|
||||
|
||||
### On decorator
|
||||
|
||||
In addition to the naming convention, message handlers may be created with the [`on`][textual.on] decorator, which turns a method into a handler for the given message or event.
|
||||
|
||||
Reference in New Issue
Block a user