Merge pull request #1193 from davep/label-widget

Add a Label widget
This commit is contained in:
Dave Pearson
2022-11-17 20:25:45 +00:00
committed by GitHub
14 changed files with 85 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
https://github.com/Textualize/textual/issues/1094
- Added Pilot.wait_for_animation
- Added `Widget.move_child` https://github.com/Textualize/textual/issues/1121
- Added a `Label` widget https://github.com/Textualize/textual/issues/1190
- Support lazy-instantiated Screens (callables in App.SCREENS) https://github.com/Textualize/textual/pull/1185
### Changed

1
docs/api/label.md Normal file
View File

@@ -0,0 +1 @@
::: textual.widgets.Label

View File

@@ -0,0 +1,12 @@
from textual.app import App, ComposeResult
from textual.widgets import Label
class LabelApp(App):
def compose(self) -> ComposeResult:
yield Label("Hello, world!")
if __name__ == "__main__":
app = LabelApp()
app.run()

33
docs/widgets/label.md Normal file
View File

@@ -0,0 +1,33 @@
# Label
A widget which displays static text, but which can also contain more complex Rich renderables.
- [ ] Focusable
- [ ] Container
## Example
The example below shows how you can use a `Label` widget to display some text.
=== "Output"
```{.textual path="docs/examples/widgets/label.py"}
```
=== "label.py"
```python
--8<-- "docs/examples/widgets/label.py"
```
## Reactive Attributes
This widget has no reactive attributes.
## Messages
This widget sends no messages.
## See Also
* [Label](../api/label.md) code reference

View File

@@ -1,14 +1,14 @@
# Static
A widget which displays static content.
Can be used for simple text labels, but can also contain more complex Rich renderables.
Can be used for Rich renderables and can also for the base for other types of widgets.
- [ ] Focusable
- [x] Container
- [ ] Container
## Example
The example below shows how you can use a `Static` widget as a simple text label.
The example below shows how you can use a `Static` widget as a simple text label (but see [Label](./label.md) as a way of displaying text).
=== "Output"
@@ -32,3 +32,4 @@ This widget sends no messages.
## See Also
* [Static](../api/static.md) code reference
* [Label](./label.md)

View File

@@ -13,7 +13,7 @@ from textual.containers import Horizontal
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widget import Widget
from textual.widgets import Footer, Button, Static
from textual.widgets import Footer, Button, Label
from textual.css.query import DOMQuery
from textual.reactive import reactive
from textual.binding import Binding
@@ -33,10 +33,10 @@ class Help(Screen):
Returns:
ComposeResult: The result of composing the help screen.
"""
yield Static(Markdown(Path(__file__).with_suffix(".md").read_text()))
yield Label(Markdown(Path(__file__).with_suffix(".md").read_text()))
class WinnerMessage(Static):
class WinnerMessage(Label):
"""Widget to tell the user they have won."""
MIN_MOVES: Final = 14
@@ -91,9 +91,9 @@ class GameHeader(Widget):
ComposeResult: The result of composing the game header.
"""
yield Horizontal(
Static(self.app.title, id="app-title"),
Static(id="moves"),
Static(id="progress"),
Label(self.app.title, id="app-title"),
Label(id="moves"),
Label(id="progress"),
)
def watch_moves(self, moves: int):
@@ -102,7 +102,7 @@ class GameHeader(Widget):
Args:
moves (int): The number of moves made.
"""
self.query_one("#moves", Static).update(f"Moves: {moves}")
self.query_one("#moves", Label).update(f"Moves: {moves}")
def watch_filled(self, filled: int):
"""Watch the on-count reactive and update when it changes.
@@ -110,7 +110,7 @@ class GameHeader(Widget):
Args:
filled (int): The number of cells that are currently on.
"""
self.query_one("#progress", Static).update(f"Filled: {filled}")
self.query_one("#progress", Label).update(f"Filled: {filled}")
class GameCell(Button):

View File

@@ -96,6 +96,7 @@ nav:
- "widgets/footer.md"
- "widgets/header.md"
- "widgets/input.md"
- "widgets/label.md"
- "widgets/static.md"
- "widgets/tree_control.md"
- API:
@@ -109,6 +110,7 @@ nav:
- "api/footer.md"
- "api/geometry.md"
- "api/header.md"
- "api/label.md"
- "api/message_pump.md"
- "api/message.md"
- "api/pilot.md"
@@ -185,13 +187,13 @@ plugins:
- blog:
- rss:
match_path: blog/posts/.*
match_path: blog/posts/.*
date_from_meta:
as_creation: date
categories:
- categories
- release
- tags
- tags
- search:
- autorefs:
- mkdocstrings:
@@ -215,10 +217,10 @@ extra_css:
extra:
social:
- icon: fontawesome/brands/twitter
- icon: fontawesome/brands/twitter
link: https://twitter.com/textualizeio
name: textualizeio on Twitter
- icon: fontawesome/brands/github
- icon: fontawesome/brands/github
link: https://github.com/textualize/textual/
name: Textual on Github
- icon: fontawesome/brands/discord

View File

@@ -1,6 +1,6 @@
from textual.app import App, ComposeResult
from textual.constants import BORDERS
from textual.widgets import Button, Static
from textual.widgets import Button, Label
from textual.containers import Vertical
@@ -48,7 +48,7 @@ class BorderApp(App):
def compose(self):
yield BorderButtons()
self.text = Static(TEXT, id="text")
self.text = Label(TEXT, id="text")
yield self.text
def on_button_pressed(self, event: Button.Pressed) -> None:

View File

@@ -68,7 +68,7 @@ ColorGroup.-active {
}
ColorLabel {
Label {
padding: 0 0 1 0;
content-align: center middle;
color: $text;

View File

@@ -2,7 +2,7 @@ from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
from textual.design import ColorSystem
from textual.widget import Widget
from textual.widgets import Button, Footer, Static
from textual.widgets import Button, Footer, Static, Label
class ColorButtons(Vertical):
@@ -28,10 +28,6 @@ class Content(Vertical):
pass
class ColorLabel(Static):
pass
class ColorsView(Vertical):
def compose(self) -> ComposeResult:
@@ -47,7 +43,7 @@ class ColorsView(Vertical):
for color_name in ColorSystem.COLOR_NAMES:
items: list[Widget] = [ColorLabel(f'"{color_name}"')]
items: list[Widget] = [Label(f'"{color_name}"')]
for level in LEVELS:
color = f"{color_name}-{level}" if level else color_name
item = ColorItem(

View File

@@ -8,7 +8,7 @@ from textual.containers import Container, Horizontal, Vertical
from textual.reactive import Reactive
from textual.scrollbar import ScrollBarRender
from textual.widget import Widget
from textual.widgets import Button, Footer, Static, Input
from textual.widgets import Button, Footer, Label, Input
VIRTUAL_SIZE = 100
WINDOW_SIZE = 10
@@ -27,7 +27,7 @@ class Bar(Widget):
animation_running = Reactive(False)
DEFAULT_CSS = """
Bar {
background: $surface;
color: $error;
@@ -37,7 +37,7 @@ class Bar(Widget):
background: $surface;
color: $success;
}
"""
def watch_animation_running(self, running: bool) -> None:
@@ -67,14 +67,14 @@ class EasingApp(App):
self.animated_bar.position = START_POSITION
duration_input = Input("1.0", placeholder="Duration", id="duration-input")
self.opacity_widget = Static(
self.opacity_widget = Label(
f"[b]Welcome to Textual![/]\n\n{TEXT}", id="opacity-widget"
)
yield EasingButtons()
yield Vertical(
Horizontal(
Static("Animation Duration:", id="label"), duration_input, id="inputs"
Label("Animation Duration:", id="label"), duration_input, id="inputs"
),
Horizontal(
self.animated_bar,

View File

@@ -15,6 +15,7 @@ if typing.TYPE_CHECKING:
from ._directory_tree import DirectoryTree
from ._footer import Footer
from ._header import Header
from ._label import Label
from ._placeholder import Placeholder
from ._pretty import Pretty
from ._static import Static
@@ -30,6 +31,7 @@ __all__ = [
"DirectoryTree",
"Footer",
"Header",
"Label",
"Placeholder",
"Pretty",
"Static",

View File

@@ -5,6 +5,7 @@ from ._checkbox import Checkbox as Checkbox
from ._directory_tree import DirectoryTree as DirectoryTree
from ._footer import Footer as Footer
from ._header import Header as Header
from ._label import Label as Label
from ._placeholder import Placeholder as Placeholder
from ._pretty import Pretty as Pretty
from ._static import Static as Static

View File

@@ -0,0 +1,7 @@
"""Provides a simple Label widget."""
from ._static import Static
class Label(Static):
"""A simple label widget for displaying text-oriented renderables."""