How can post a message after a group of widget get mounted? #3332
-
Hello everyone, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If you have a collection of widgets, that would suggest they're inside a container of some sort. When that container gets its You could explore this with something like this: from dataclasses import dataclass
from textual import on
from textual.app import App, ComposeResult
from textual.containers import Horizontal, VerticalScroll
from textual.message import Message
from textual.widget import Widget
from textual.widgets import Button, Log
@dataclass
class CountMessage(Message):
widget: Widget
class CheckingButton(Button):
def on_mount(self) -> None:
self.post_message(CountMessage(self))
class CollectionOfWidgets(VerticalScroll):
DEFAULT_CSS = """
CollectionOfWidgets Button {
width: 1fr;
}
"""
def compose(self) -> ComposeResult:
for n in range(100):
yield CheckingButton(f"This is button {n}", id=f"button-{n}")
def on_mount(self) -> None:
self.post_message(CountMessage(self))
class KnowWhenOthersAreMountedApp(App[None]):
def compose(self) -> ComposeResult:
with Horizontal():
yield CollectionOfWidgets()
yield Log()
@on(CountMessage)
def log_count(self, event: CountMessage) -> None:
self.query_one(Log).write_line(f"Mounted: {event.widget!r}")
if len(event.widget.children):
self.query_one(Log).write_line(f" It also has {len(event.widget.children)} children")
if __name__ == "__main__":
KnowWhenOthersAreMountedApp().run() When I run that, each of the buttons get their |
Beta Was this translation helpful? Give feedback.
If you have a collection of widgets, that would suggest they're inside a container of some sort. When that container gets its
on_mount
message, the children will be mounted.You could explore this with something like this: