mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
widgets docs
This commit is contained in:
9
docs/examples/guide/widgets/fizzbuzz.css
Normal file
9
docs/examples/guide/widgets/fizzbuzz.css
Normal file
@@ -0,0 +1,9 @@
|
||||
Screen {
|
||||
layout: center;
|
||||
}
|
||||
|
||||
FizzBuzz {
|
||||
width: auto;
|
||||
height: auto;
|
||||
background: $panel;
|
||||
}
|
||||
30
docs/examples/guide/widgets/fizzbuzz.py
Normal file
30
docs/examples/guide/widgets/fizzbuzz.py
Normal 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()
|
||||
3
docs/examples/guide/widgets/hello01.css
Normal file
3
docs/examples/guide/widgets/hello01.css
Normal file
@@ -0,0 +1,3 @@
|
||||
Screen {
|
||||
layout: center;
|
||||
}
|
||||
19
docs/examples/guide/widgets/hello01.py
Normal file
19
docs/examples/guide/widgets/hello01.py
Normal 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()
|
||||
13
docs/examples/guide/widgets/hello02.css
Normal file
13
docs/examples/guide/widgets/hello02.css
Normal 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;
|
||||
}
|
||||
21
docs/examples/guide/widgets/hello02.py
Normal file
21
docs/examples/guide/widgets/hello02.py
Normal 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()
|
||||
12
docs/examples/guide/widgets/hello03.css
Normal file
12
docs/examples/guide/widgets/hello03.css
Normal 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;
|
||||
}
|
||||
48
docs/examples/guide/widgets/hello03.py
Normal file
48
docs/examples/guide/widgets/hello03.py
Normal 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()
|
||||
Reference in New Issue
Block a user