refine compose

This commit is contained in:
Will McGugan
2022-09-05 11:02:37 +01:00
parent 7db856d404
commit 54539a90d1
2 changed files with 14 additions and 14 deletions

View File

@@ -5,9 +5,9 @@ from textual.widgets import Static, Button
class QuestionApp(App[str]): class QuestionApp(App[str]):
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
yield Static("Do you love Textual?") yield Static("Do you love Textual?")
yield (yes := Button("Yes", id="yes")) yes = yield Button("Yes", id="yes")
yes.variant = "primary" yes.variant = "primary"
yield (no := Button("No", id="no")) no = yield Button("No", id="no")
no.variant = "error" no.variant = "error"
def on_button_pressed(self, event: Button.Pressed) -> None: def on_button_pressed(self, event: Button.Pressed) -> None:

View File

@@ -4,7 +4,6 @@ from types import GeneratorType
from typing import TYPE_CHECKING, Iterable from typing import TYPE_CHECKING, Iterable
if TYPE_CHECKING: if TYPE_CHECKING:
from .app import ComposeResult from .app import ComposeResult
from .widget import Widget from .widget import Widget
@@ -28,17 +27,18 @@ def _compose(compose_result: ComposeResult) -> Iterable[Widget]:
or a generator. or a generator.
Returns: Returns:
Iterable[Widget]: In iterable if widgets. Iterable[Widget]: An iterable if widgets.
""" """
if not isinstance(compose_result, GeneratorType): if isinstance(compose_result, GeneratorType):
return compose_result
try: try:
widget = next(compose_result) widget = next(compose_result)
send = compose_result.send
while True: while True:
yield widget yield widget
widget = compose_result.send(widget) widget = send(widget)
except StopIteration: except StopIteration:
pass pass
else:
yield from compose_result