button tweaks

This commit is contained in:
Will McGugan
2022-09-18 22:17:15 +01:00
parent d0293c2c89
commit 826095727c
2 changed files with 12 additions and 5 deletions

View File

@@ -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())

View File

@@ -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: