-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4075 from Textualize/cancelled-event
Data binding and more
- Loading branch information
Showing
25 changed files
with
840 additions
and
39 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
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,67 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.containers import Horizontal | ||
from textual.reactive import reactive, var | ||
from textual.widgets import Label | ||
|
||
GREETINGS = [ | ||
"Bonjour", | ||
"Hola", | ||
"こんにちは", | ||
"你好", | ||
"안녕하세요", | ||
"Hello", | ||
] | ||
|
||
|
||
class Greeter(Horizontal): | ||
"""Display a greeting and a name.""" | ||
|
||
DEFAULT_CSS = """ | ||
Greeter { | ||
width: auto; | ||
height: 1; | ||
& Label { | ||
margin: 0 1; | ||
} | ||
} | ||
""" | ||
greeting: reactive[str] = reactive("") | ||
who: reactive[str] = reactive("") | ||
|
||
def __init__(self, greeting: str = "Hello", who: str = "World!") -> None: | ||
super().__init__() | ||
self.greeting = greeting # (1)! | ||
self.who = who | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label(self.greeting, id="greeting") | ||
yield Label(self.who, id="name") | ||
|
||
def watch_greeting(self, greeting: str) -> None: | ||
self.query_one("#greeting", Label).update(greeting) # (2)! | ||
|
||
def watch_who(self, who: str) -> None: | ||
self.query_one("#who", Label).update(who) | ||
|
||
|
||
class NameApp(App): | ||
|
||
CSS = """ | ||
Screen { | ||
align: center middle; | ||
} | ||
""" | ||
greeting_no: var[int] = var(0) | ||
BINDINGS = [("space", "greeting")] | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Greeter(who="Textual") | ||
|
||
def action_greeting(self) -> None: | ||
self.greeting_no = (self.greeting_no + 1) % len(GREETINGS) | ||
self.query_one(Greeter).greeting = GREETINGS[self.greeting_no] | ||
|
||
|
||
if __name__ == "__main__": | ||
app = NameApp() | ||
app.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.containers import Horizontal | ||
from textual.reactive import reactive, var | ||
from textual.widgets import Label | ||
|
||
GREETINGS = [ | ||
"Bonjour", | ||
"Hola", | ||
"こんにちは", | ||
"你好", | ||
"안녕하세요", | ||
"Hello", | ||
] | ||
|
||
|
||
class Greeter(Horizontal): | ||
"""Display a greeting and a name.""" | ||
|
||
DEFAULT_CSS = """ | ||
Greeter { | ||
width: auto; | ||
height: 1; | ||
& Label { | ||
margin: 0 1; | ||
} | ||
} | ||
""" | ||
greeting: reactive[str] = reactive("") | ||
who: reactive[str] = reactive("") | ||
|
||
def __init__(self, greeting: str = "Hello", who: str = "World!") -> None: | ||
super().__init__() | ||
self.set_reactive(Greeter.greeting, greeting) # (1)! | ||
self.set_reactive(Greeter.who, who) | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label(self.greeting, id="greeting") | ||
yield Label(self.who, id="name") | ||
|
||
def watch_greeting(self, greeting: str) -> None: | ||
self.query_one("#greeting", Label).update(greeting) | ||
|
||
def watch_who(self, who: str) -> None: | ||
self.query_one("#who", Label).update(who) | ||
|
||
|
||
class NameApp(App): | ||
|
||
CSS = """ | ||
Screen { | ||
align: center middle; | ||
} | ||
""" | ||
greeting_no: var[int] = var(0) | ||
BINDINGS = [("space", "greeting")] | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Greeter(who="Textual") | ||
|
||
def action_greeting(self) -> None: | ||
self.greeting_no = (self.greeting_no + 1) % len(GREETINGS) | ||
self.query_one(Greeter).greeting = GREETINGS[self.greeting_no] | ||
|
||
|
||
if __name__ == "__main__": | ||
app = NameApp() | ||
app.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from datetime import datetime | ||
|
||
from pytz import timezone | ||
|
||
from textual.app import App, ComposeResult | ||
from textual.reactive import reactive | ||
from textual.widget import Widget | ||
from textual.widgets import Digits, Label | ||
|
||
|
||
class WorldClock(Widget): | ||
|
||
time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def __init__(self, timezone: str) -> None: | ||
self.timezone = timezone | ||
super().__init__() | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label(self.timezone) | ||
yield Digits() | ||
|
||
def watch_time(self, time: datetime) -> None: | ||
localized_time = time.astimezone(timezone(self.timezone)) | ||
self.query_one(Digits).update(localized_time.strftime("%H:%M:%S")) | ||
|
||
|
||
class WorldClockApp(App): | ||
CSS_PATH = "world_clock01.tcss" | ||
|
||
time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def compose(self) -> ComposeResult: | ||
yield WorldClock("Europe/London") | ||
yield WorldClock("Europe/Paris") | ||
yield WorldClock("Asia/Tokyo") | ||
|
||
def update_time(self) -> None: | ||
self.time = datetime.now() | ||
|
||
def watch_time(self, time: datetime) -> None: | ||
for world_clock in self.query(WorldClock): # (1)! | ||
world_clock.time = time | ||
|
||
def on_mount(self) -> None: | ||
self.update_time() | ||
self.set_interval(1, self.update_time) | ||
|
||
|
||
if __name__ == "__main__": | ||
app = WorldClockApp() | ||
app.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Screen { | ||
align: center middle; | ||
} | ||
|
||
WorldClock { | ||
width: auto; | ||
height: auto; | ||
padding: 1 2; | ||
background: $panel; | ||
border: wide $background; | ||
|
||
& Digits { | ||
width: auto; | ||
color: $secondary; | ||
} | ||
} |
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,47 @@ | ||
from datetime import datetime | ||
|
||
from pytz import timezone | ||
|
||
from textual.app import App, ComposeResult | ||
from textual.reactive import reactive | ||
from textual.widget import Widget | ||
from textual.widgets import Digits, Label | ||
|
||
|
||
class WorldClock(Widget): | ||
|
||
time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def __init__(self, timezone: str) -> None: | ||
self.timezone = timezone | ||
super().__init__() | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label(self.timezone) | ||
yield Digits() | ||
|
||
def watch_time(self, time: datetime) -> None: | ||
localized_time = time.astimezone(timezone(self.timezone)) | ||
self.query_one(Digits).update(localized_time.strftime("%H:%M:%S")) | ||
|
||
|
||
class WorldClockApp(App): | ||
CSS_PATH = "world_clock01.tcss" | ||
|
||
time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def compose(self) -> ComposeResult: | ||
yield WorldClock("Europe/London").data_bind(WorldClockApp.time) # (1)! | ||
yield WorldClock("Europe/Paris").data_bind(WorldClockApp.time) | ||
yield WorldClock("Asia/Tokyo").data_bind(WorldClockApp.time) | ||
|
||
def update_time(self) -> None: | ||
self.time = datetime.now() | ||
|
||
def on_mount(self) -> None: | ||
self.update_time() | ||
self.set_interval(1, self.update_time) | ||
|
||
|
||
if __name__ == "__main__": | ||
WorldClockApp().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from datetime import datetime | ||
|
||
from pytz import timezone | ||
|
||
from textual.app import App, ComposeResult | ||
from textual.reactive import reactive | ||
from textual.widget import Widget | ||
from textual.widgets import Digits, Label | ||
|
||
|
||
class WorldClock(Widget): | ||
|
||
clock_time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def __init__(self, timezone: str) -> None: | ||
self.timezone = timezone | ||
super().__init__() | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label(self.timezone) | ||
yield Digits() | ||
|
||
def watch_clock_time(self, time: datetime) -> None: | ||
localized_time = time.astimezone(timezone(self.timezone)) | ||
self.query_one(Digits).update(localized_time.strftime("%H:%M:%S")) | ||
|
||
|
||
class WorldClockApp(App): | ||
CSS_PATH = "world_clock01.tcss" | ||
|
||
time: reactive[datetime] = reactive(datetime.now) | ||
|
||
def compose(self) -> ComposeResult: | ||
yield WorldClock("Europe/London").data_bind( | ||
clock_time=WorldClockApp.time # (1)! | ||
) | ||
yield WorldClock("Europe/Paris").data_bind(clock_time=WorldClockApp.time) | ||
yield WorldClock("Asia/Tokyo").data_bind(clock_time=WorldClockApp.time) | ||
|
||
def update_time(self) -> None: | ||
self.time = datetime.now() | ||
|
||
def on_mount(self) -> None: | ||
self.update_time() | ||
self.set_interval(1, self.update_time) | ||
|
||
|
||
if __name__ == "__main__": | ||
WorldClockApp().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
Oops, something went wrong.