widgets docs

This commit is contained in:
Will McGugan
2022-09-21 17:18:50 +01:00
parent 24d4491bf8
commit 9ab01d3c54
16 changed files with 293 additions and 158 deletions

View File

@@ -0,0 +1,9 @@
Screen {
layout: center;
}
FizzBuzz {
width: auto;
height: auto;
background: $panel;
}

View File

@@ -0,0 +1,30 @@
from rich.table import Table
from textual.app import App, ComposeResult
from textual.widgets import Static
class FizzBuzz(Static):
def on_mount(self) -> None:
table = Table("Number", "Fizz?", "Buzz?")
for n in range(1, 16):
fizz = not n % 3
buzz = not n % 5
table.add_row(
str(n),
"fizz" if fizz else "",
"buzz" if buzz else "",
)
self.update(table)
class FizzBuzzApp(App):
CSS_PATH = "fizzbuzz.css"
def compose(self) -> ComposeResult:
yield FizzBuzz()
if __name__ == "__main__":
app = FizzBuzzApp()
app.run()

View File

@@ -0,0 +1,3 @@
Screen {
layout: center;
}

View File

@@ -0,0 +1,19 @@
from textual.app import App, ComposeResult, RenderResult
from textual.widget import Widget
class Hello(Widget):
"""Display a greeting."""
def render(self) -> RenderResult:
return "Hello, [b]World[/b]!"
class CustomApp(App):
def compose(self) -> ComposeResult:
yield Hello()
if __name__ == "__main__":
app = CustomApp()
app.run()

View File

@@ -0,0 +1,13 @@
Screen {
layout: center;
}
Hello {
width: 40;
height: 9;
padding: 1 2;
background: $panel;
color: $text;
border: $secondary tall;
content-align: center middle;
}

View File

@@ -0,0 +1,21 @@
from textual.app import App, ComposeResult, RenderResult
from textual.widget import Widget
class Hello(Widget):
"""Display a greeting."""
def render(self) -> RenderResult:
return "Hello, [b]World[/b]!"
class CustomApp(App):
CSS_PATH = "hello02.css"
def compose(self) -> ComposeResult:
yield Hello()
if __name__ == "__main__":
app = CustomApp()
app.run()

View File

@@ -0,0 +1,12 @@
Screen {
layout: center;
}
Hello {
width: 40;
height: 9;
padding: 1 2;
background: $panel;
border: $secondary tall;
content-align: center middle;
}

View File

@@ -0,0 +1,48 @@
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.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()