mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Drop explicit sender attribute from messages (#1940)
* remove sender * removed priority post * timer fix * test fixes * drop async version of post_message * extended docs * fix no app * Added control properties * changelog * changelog * changelog * fix for stopping timers * changelog * added aliases to radio and checkbox * Drop sender from Message init * drop time * drop cast * Added aliases
This commit is contained in:
@@ -10,9 +10,9 @@ class ColorButton(Static):
|
||||
class Selected(Message):
|
||||
"""Color selected message."""
|
||||
|
||||
def __init__(self, sender: MessageTarget, color: Color) -> None:
|
||||
def __init__(self, color: Color) -> None:
|
||||
self.color = color
|
||||
super().__init__(sender)
|
||||
super().__init__()
|
||||
|
||||
def __init__(self, color: Color) -> None:
|
||||
self.color = color
|
||||
@@ -24,9 +24,9 @@ class ColorButton(Static):
|
||||
self.styles.background = Color.parse("#ffffff33")
|
||||
self.styles.border = ("tall", self.color)
|
||||
|
||||
async def on_click(self) -> None:
|
||||
def on_click(self) -> None:
|
||||
# The post_message method sends an event to be handled in the DOM
|
||||
await self.post_message(self.Selected(self, self.color))
|
||||
self.post_message(self.Selected(self.color))
|
||||
|
||||
def render(self) -> str:
|
||||
return str(self.color)
|
||||
|
||||
@@ -107,16 +107,11 @@ The message class is defined within the widget class itself. This is not strictl
|
||||
- It reduces the amount of imports. If you import `ColorButton`, you have access to the message class via `ColorButton.Selected`.
|
||||
- It creates a namespace for the handler. So rather than `on_selected`, the handler name becomes `on_color_button_selected`. This makes it less likely that your chosen name will clash with another message.
|
||||
|
||||
### Sending messages
|
||||
|
||||
## Sending messages
|
||||
|
||||
In the previous example we used [post_message()][textual.message_pump.MessagePump.post_message] to send an event to its parent. We could also have used [post_message_no_wait()][textual.message_pump.MessagePump.post_message_no_wait] for non async code. Sending messages in this way allows you to write custom widgets without needing to know in what context they will be used.
|
||||
|
||||
There are other ways of sending (posting) messages, which you may need to use less frequently.
|
||||
|
||||
- [post_message][textual.message_pump.MessagePump.post_message] To post a message to a particular widget.
|
||||
- [post_message_no_wait][textual.message_pump.MessagePump.post_message_no_wait] The non-async version of `post_message`.
|
||||
To send a message call the [post_message()][textual.message_pump.MessagePump.post_message] method. This will place a message on the widget's message queue and run any message handlers.
|
||||
|
||||
It is common for widgets to send messages to themselves, and allow them to bubble. This is so a base class has an opportunity to handle the message. We do this in the example above, which means a subclass could add a `on_color_button_selected` if it wanted to handle the message itself.
|
||||
|
||||
## Preventing messages
|
||||
|
||||
|
||||
Reference in New Issue
Block a user