-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Document DOMNode.watch * Create standalone example. Addresses review comment: #3724 (review). * Create standalone example. Addresses review comment: #3724 (review).
- Loading branch information
1 parent
370f5f7
commit 9a9d534
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.reactive import reactive | ||
from textual.widget import Widget | ||
from textual.widgets import Button, Label, ProgressBar | ||
|
||
|
||
class Counter(Widget): | ||
DEFAULT_CSS = "Counter { height: auto; }" | ||
counter = reactive(0) # (1)! | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label() | ||
yield Button("+10") | ||
|
||
def on_button_pressed(self) -> None: | ||
self.counter += 10 | ||
|
||
def watch_counter(self, counter_value: int): | ||
self.query_one(Label).update(str(counter_value)) | ||
|
||
|
||
class WatchApp(App[None]): | ||
def compose(self) -> ComposeResult: | ||
yield Counter() | ||
yield ProgressBar(total=100, show_eta=False) | ||
|
||
def on_mount(self): | ||
def update_progress(counter_value: int): # (2)! | ||
self.query_one(ProgressBar).update(progress=counter_value) | ||
|
||
self.watch(self.query_one(Counter), "counter", update_progress) # (3)! | ||
|
||
|
||
if __name__ == "__main__": | ||
WatchApp().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters