-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_message.py
57 lines (44 loc) · 1.36 KB
/
custom_message.py
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
55
56
57
import time
from textual import work
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.message import Message
from textual.screen import Screen
from textual.widgets import Label
class MyLabel(Label):
class UpdateMsg(Message):
def __init__(self, msg: str) -> None:
super().__init__()
self.msg = msg
class MyScreen(Screen):
def compose(self) -> ComposeResult:
with Vertical():
yield MyLabel("First!")
# doesn't work
def on_my_label_update_msg(self, message: MyLabel.UpdateMsg) -> None:
self.query_one("MyLabel").update(message.msg)
class CustomMessageApp(App[None]):
CSS = """
Vertical {
align: center middle;
}
Label {
padding: 1 2;
border: solid $secondary;
color: $text;
}
"""
def on_mount(self):
self.push_screen(MyScreen())
self.update()
@work(thread=True)
def update(self) -> None:
time.sleep(1)
self.post_message(MyLabel.UpdateMsg("First post!"))
time.sleep(1)
self.call_from_thread(self.app.pop_screen)
# does work
# def on_my_label_update_msg(self, message: MyLabel.UpdateMsg) -> None:
# self.query_one("MyLabel").update(message.msg)
if __name__ == "__main__":
CustomMessageApp().run()