-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_bind_after_compose.py
More file actions
54 lines (38 loc) · 1.38 KB
/
Copy pathdata_bind_after_compose.py
File metadata and controls
54 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from textual import on
from textual.app import App, ComposeResult
from textual.message import Message
from textual.reactive import reactive
from textual.widget import Widget
from textual.widgets import Button, Label
class L3(Widget):
animal_sound = reactive("Meow")
def compose(self) -> ComposeResult:
yield Label(id="label3")
def watch_animal_sound(self, value: str) -> None:
self.query_one("#label3").update(self.animal_sound * 3)
class L2(Widget):
class MountAnimalWidget(Message):
def __init__(self, widget: Widget) -> None:
super().__init__()
self.widget = widget
animal_sound = reactive("Woof")
def compose(self) -> ComposeResult:
yield Label(id="label2")
def watch_animal_sound(self, value: str) -> None:
self.query_one("#label2").update(self.animal_sound * 2)
@on(MountAnimalWidget)
def mount_animal_widget(self, event: MountAnimalWidget) -> None:
self.mount(event.widget.data_bind(L2.animal_sound))
class MainApp(App):
CSS = """
Widget {
height: auto;
}
"""
animal_sound = reactive("Meh")
def compose(self) -> ComposeResult:
yield L2().data_bind(MainApp.animal_sound)
yield Button("Mount L3")
def on_button_pressed(self) -> None:
self.query_one(L2).post_message(L2.MountAnimalWidget(L3()))
MainApp().run()