mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
event docs
This commit is contained in:
48
docs/examples/events/custom01.py
Normal file
48
docs/examples/events/custom01.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.color import Color
|
||||
from textual.message import Message, MessageTarget
|
||||
from textual.widgets import Static
|
||||
|
||||
|
||||
class ColorButton(Static):
|
||||
"""A color button."""
|
||||
|
||||
class Selected(Message):
|
||||
"""Color selected message."""
|
||||
|
||||
def __init__(self, sender: MessageTarget, color: Color) -> None:
|
||||
self.color = color
|
||||
super().__init__(sender)
|
||||
|
||||
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)
|
||||
|
||||
async def on_click(self) -> None:
|
||||
# The emit method sends an event to a widget's parent
|
||||
await self.emit(self.Selected(self, 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)
|
||||
|
||||
|
||||
app = ColorApp()
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
Reference in New Issue
Block a user