mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
refine compose
This commit is contained in:
@@ -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:
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user