From 4437c7b6403a63fac59c7e04f7bbcc7a31df7304 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 22 Sep 2022 18:16:02 +0100 Subject: [PATCH] hello examples --- docs/examples/guide/widgets/hello04.css | 3 ++ docs/examples/guide/widgets/hello04.py | 59 +++++++++++++++++++++++++ docs/examples/guide/widgets/hello05.css | 12 +++++ docs/examples/guide/widgets/hello05.py | 45 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 docs/examples/guide/widgets/hello04.css create mode 100644 docs/examples/guide/widgets/hello04.py create mode 100644 docs/examples/guide/widgets/hello05.css create mode 100644 docs/examples/guide/widgets/hello05.py diff --git a/docs/examples/guide/widgets/hello04.css b/docs/examples/guide/widgets/hello04.css new file mode 100644 index 000000000..8fd85db4d --- /dev/null +++ b/docs/examples/guide/widgets/hello04.css @@ -0,0 +1,3 @@ +Screen { + layout: center; +} diff --git a/docs/examples/guide/widgets/hello04.py b/docs/examples/guide/widgets/hello04.py new file mode 100644 index 000000000..f026b5880 --- /dev/null +++ b/docs/examples/guide/widgets/hello04.py @@ -0,0 +1,59 @@ +from itertools import cycle + +from textual.app import App, ComposeResult +from textual.widgets import Static + + +hellos = cycle( + [ + "Hola", + "Bonjour", + "Guten tag", + "Salve", + "Nǐn hǎo", + "Olá", + "Asalaam alaikum", + "Konnichiwa", + "Anyoung haseyo", + "Zdravstvuyte", + "Hello", + ] +) + + +class Hello(Static): + """Display a greeting.""" + + DEFAULT_CSS = """ + Hello { + width: 40; + height: 9; + padding: 1 2; + background: $panel; + border: $secondary tall; + content-align: center middle; + } + """ + + def on_mount(self) -> None: + self.next_word() + + def on_click(self) -> None: + self.next_word() + + def next_word(self) -> None: + """Get a new hello and update the content area.""" + hello = next(hellos) + self.update(f"{hello}, [b]World[/b]!") + + +class CustomApp(App): + CSS_PATH = "hello03.css" + + def compose(self) -> ComposeResult: + yield Hello() + + +if __name__ == "__main__": + app = CustomApp() + app.run() diff --git a/docs/examples/guide/widgets/hello05.css b/docs/examples/guide/widgets/hello05.css new file mode 100644 index 000000000..44847f8ac --- /dev/null +++ b/docs/examples/guide/widgets/hello05.css @@ -0,0 +1,12 @@ +Screen { + layout: center; +} + +Hello { + width: 40; + height: 9; + padding: 1 2; + background: $panel; + border: $secondary tall; + content-align: center middle; +} diff --git a/docs/examples/guide/widgets/hello05.py b/docs/examples/guide/widgets/hello05.py new file mode 100644 index 000000000..1430138b8 --- /dev/null +++ b/docs/examples/guide/widgets/hello05.py @@ -0,0 +1,45 @@ +from itertools import cycle + +from textual.app import App, ComposeResult +from textual.widgets import Static + + +hellos = cycle( + [ + "Hola", + "Bonjour", + "Guten tag", + "Salve", + "Nǐn hǎo", + "Olá", + "Asalaam alaikum", + "Konnichiwa", + "Anyoung haseyo", + "Zdravstvuyte", + "Hello", + ] +) + + +class Hello(Static): + """Display a greeting.""" + + def on_mount(self) -> None: + self.action_next_word() + + def action_next_word(self) -> None: + """Get a new hello and update the content area.""" + hello = next(hellos) + self.update(f"[@click='next_word']{hello}[/], [b]World[/b]!") + + +class CustomApp(App): + CSS_PATH = "hello05.css" + + def compose(self) -> ComposeResult: + yield Hello() + + +if __name__ == "__main__": + app = CustomApp() + app.run()