Applying styles to button

This commit is contained in:
Darren Burns
2022-05-10 11:22:04 +01:00
parent 055c43660c
commit b61ee05c98
4 changed files with 19 additions and 8 deletions

View File

@@ -0,0 +1,8 @@
#foo {
text-style: underline;
background: rebeccapurple;
}
#foo:hover {
background: greenyellow;
}

View File

@@ -18,7 +18,7 @@ class ButtonsApp(App[str]):
self.exit(event.button.id)
app = ButtonsApp(log_path="textual.log", log_verbosity=2)
app = ButtonsApp(log_path="textual.log", css_path="buttons.css", log_verbosity=2)
if __name__ == "__main__":
result = app.run()

View File

@@ -464,7 +464,6 @@ class Widget(DOMNode):
Returns:
RenderableType: A new renderable.
"""
renderable = self.render(self.styles.rich_style)
styles = self.styles
@@ -478,13 +477,13 @@ class Widget(DOMNode):
horizontal, vertical = content_align
renderable = Align(renderable, horizontal, vertical=vertical)
renderable = Padding(renderable, styles.padding)
renderable_text_style = parent_text_style + text_style
if renderable_text_style:
style = Style.from_color(text_style.color, text_style.bgcolor)
renderable = Styled(renderable, style)
renderable = Padding(renderable, styles.padding)
if styles.border:
renderable = Border(
renderable,

View File

@@ -4,7 +4,7 @@ from typing import cast
from rich.console import RenderableType
from rich.style import Style
from rich.text import Text
from rich.text import Text, TextType
from .. import events
from ..message import Message
@@ -49,7 +49,7 @@ class Button(Widget, can_focus=True):
def __init__(
self,
label: RenderableType | None = None,
label: TextType | None = None,
disabled: bool = False,
*,
name: str | None = None,
@@ -58,7 +58,11 @@ class Button(Widget, can_focus=True):
):
super().__init__(name=name, id=id, classes=classes)
self.label = self.css_identifier_styled if label is None else label
if label is None:
label = self.css_identifier_styled
self.label: Text = self.validate_label(label)
self.disabled = disabled
if disabled:
self.add_class("-disabled")
@@ -72,7 +76,7 @@ class Button(Widget, can_focus=True):
return label
def render(self, style: Style) -> RenderableType:
return self.label
return Text.styled(self.label.plain, style)
async def on_click(self, event: events.Click) -> None:
event.stop()