mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
* Add on decorator * decorator code * docs for on decorator * Examples * test errors * simplify listing * words * changelog * Update docs/guide/events.md Co-authored-by: Dave Pearson <davep@davep.org> * Update docs/guide/events.md Co-authored-by: Dave Pearson <davep@davep.org> * Update docs/examples/events/on_decorator.css Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com> * Update docs/guide/events.md Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com> * rewording * comment * clarification * Added note --------- Co-authored-by: Dave Pearson <davep@davep.org> Co-authored-by: Rodrigo Girão Serrão <5621605+rodrigogiraoserrao@users.noreply.github.com>
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from textual.app import App, ComposeResult
|
|
from textual.color import Color
|
|
from textual.message import Message
|
|
from textual.widgets import Static
|
|
|
|
|
|
class ColorButton(Static):
|
|
"""A color button."""
|
|
|
|
class Selected(Message):
|
|
"""Color selected message."""
|
|
|
|
def __init__(self, color: Color) -> None:
|
|
self.color = color
|
|
super().__init__()
|
|
|
|
def __init__(self, color: Color) -> None:
|
|
self.color = color
|
|
super().__init__()
|
|
|
|
def on_mount(self) -> None:
|
|
self.styles.margin = (1, 2)
|
|
self.styles.content_align = ("center", "middle")
|
|
self.styles.background = Color.parse("#ffffff33")
|
|
self.styles.border = ("tall", self.color)
|
|
|
|
def on_click(self) -> None:
|
|
# The post_message method sends an event to be handled in the DOM
|
|
self.post_message(self.Selected(self.color))
|
|
|
|
def render(self) -> str:
|
|
return str(self.color)
|
|
|
|
|
|
class ColorApp(App):
|
|
def compose(self) -> ComposeResult:
|
|
yield ColorButton(Color.parse("#008080"))
|
|
yield ColorButton(Color.parse("#808000"))
|
|
yield ColorButton(Color.parse("#E9967A"))
|
|
yield ColorButton(Color.parse("#121212"))
|
|
|
|
def on_color_button_selected(self, message: ColorButton.Selected) -> None:
|
|
self.screen.styles.animate("background", message.color, duration=0.5)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = ColorApp()
|
|
app.run()
|