-
Hi, from textual import log
from textual.app import App, ComposeResult
from textual.message import Message, MessageTarget
from textual.widgets import Static, ListView, ListItem
class SomeList(ListView):
"""First list"""
class Update(Message):
"""Custom message used to send data to another widget"""
def __init__(self, sender: MessageTarget, data: str):
super().__init__(sender)
self.data = data
def compose(self) -> ComposeResult:
yield ListItem(Static("Entry one"))
yield ListItem(Static("Entry two"))
yield ListItem(Static("Entry three"))
def on_list_view_selected(self, message):
log(f"SomeList.on_list_view_selected")
# post an `Update` message with the string of the static inside as data attribute
self.post_message(self.Update(self, message.item.children[0].renderable))
class AnotherList(ListView):
"""Second list"""
def compose(self) -> ComposeResult:
yield ListItem(Static("Hello"))
def on_some_list_update(self, message: SomeList.Update):
log("AnotherList.on_some_list_update")
self.append(ListItem(Static(f"New: {message.data}")))
class MyApp(App):
def compose(self) -> ComposeResult:
yield Static("FIRST LIST")
yield SomeList()
yield Static("SECOND LIST")
yield AnotherList()
def on_some_list_update(self):
"""This should be used to forward the message to `AnotherList`"""
log("MyApp.on_some_list_update")
def on_list_view_update(self):
"""Same as above as I don't know which name is the correct one"""
log("MyApp.on_list_view_update")
if __name__ == "__main__":
app = MyApp()
app.run() First Question: Second Question:
I expected the first log message but don't know what it means that Thanks in advance :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There was a change to post_message recently. The code looks fine, but the error suggests you have an old version of Textual. Try updating, you might find your example works as expected. |
Beta Was this translation helpful? Give feedback.
-
Further to what Will says above, I think you may also have run into a small issue we do have from time to time: it's not uncommon that the documentation for the For the moment things should work for you if you use Hope that makes sense? Long story short: one of those rare times where reading the fine manual caused the problem, and it's our fault. ;-) |
Beta Was this translation helpful? Give feedback.
Further to what Will says above, I think you may also have run into a small issue we do have from time to time: it's not uncommon that the documentation for the
main
branch "leaks" onto the website before the next release is made. From what I can see of your problem you're (rightly, as per the docs right now) callingpost_message
when you likely really wantpost_message_no_wait
; this is assuming you're working with 0.13 of Textual rather than a clone ofmain
.For the moment things should work for you if you use
post_message_no_wait
; but note that this will change in 0.14, where it will bepost_message
and you won't need toawait
it.Hope that makes sense?
Long story short: one of those ra…