mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
button tweaks
This commit is contained in:
@@ -3,7 +3,7 @@ from textual.app import App, ComposeResult
|
|||||||
from textual.widgets import Button, Static
|
from textual.widgets import Button, Static
|
||||||
|
|
||||||
|
|
||||||
class ButtonsApp(App):
|
class ButtonsApp(App[str]):
|
||||||
CSS_PATH = "button.css"
|
CSS_PATH = "button.css"
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
@@ -26,10 +26,11 @@ class ButtonsApp(App):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_button_pressed(self, _event: Button.Pressed) -> None:
|
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||||
self.app.bell()
|
self.app.bell()
|
||||||
|
self.exit(str(event.button))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = ButtonsApp()
|
app = ButtonsApp()
|
||||||
result = app.run()
|
print(app.run())
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ if sys.version_info >= (3, 8):
|
|||||||
else:
|
else:
|
||||||
from typing_extensions import Literal # pragma: no cover
|
from typing_extensions import Literal # pragma: no cover
|
||||||
|
|
||||||
|
import rich.repr
|
||||||
from rich.console import RenderableType
|
from rich.console import RenderableType
|
||||||
from rich.text import Text, TextType
|
from rich.text import Text, TextType
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ from .. import events
|
|||||||
from ..css._error_tools import friendly_list
|
from ..css._error_tools import friendly_list
|
||||||
from ..message import Message
|
from ..message import Message
|
||||||
from ..reactive import Reactive
|
from ..reactive import Reactive
|
||||||
from ..widget import Widget
|
from ..widgets import Static
|
||||||
|
|
||||||
ButtonVariant = Literal["default", "primary", "success", "warning", "error"]
|
ButtonVariant = Literal["default", "primary", "success", "warning", "error"]
|
||||||
_VALID_BUTTON_VARIANTS = {"default", "primary", "success", "warning", "error"}
|
_VALID_BUTTON_VARIANTS = {"default", "primary", "success", "warning", "error"}
|
||||||
@@ -26,7 +27,7 @@ class InvalidButtonVariant(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Button(Widget, can_focus=True):
|
class Button(Static, can_focus=True):
|
||||||
"""A simple clickable button."""
|
"""A simple clickable button."""
|
||||||
|
|
||||||
DEFAULT_CSS = """
|
DEFAULT_CSS = """
|
||||||
@@ -196,6 +197,11 @@ class Button(Widget, can_focus=True):
|
|||||||
variant = Reactive.init("default")
|
variant = Reactive.init("default")
|
||||||
disabled = Reactive(False)
|
disabled = Reactive(False)
|
||||||
|
|
||||||
|
def __rich_repr__(self) -> rich.repr.Result:
|
||||||
|
yield from super().__rich_repr__()
|
||||||
|
yield "variant", self.variant, "default"
|
||||||
|
yield "disabled", self.disabled, False
|
||||||
|
|
||||||
def watch_mouse_over(self, value: bool) -> None:
|
def watch_mouse_over(self, value: bool) -> None:
|
||||||
"""Update from CSS if mouse over state changes."""
|
"""Update from CSS if mouse over state changes."""
|
||||||
if not self.disabled:
|
if not self.disabled:
|
||||||
|
|||||||
Reference in New Issue
Block a user