Is there a way to use $boost, $accent, $panel inside of python code? #5146
-
So I noticed that when I try to use $boost, $panel, or $accent inside of a widget, for instance: self.set_styles("background: $boost;") self.styles.background = "$boost" I get an error saying $boost / $panel / $accent is not a color. I was wondering if there's a way to do this properly? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The problem is that the I don't know if this is the "proper" way, but using the from textual.app import App, ComposeResult
from textual.containers import Grid
from textual.widgets import Static
COLOR_NAMES = [
"primary",
"secondary",
"background",
"primary-background",
"secondary-background",
"surface",
"panel",
"boost",
"warning",
"error",
"success",
"accent",
]
class ExampleApp(App):
CSS = """
Static {
height: 3;
color: $text;
content-align: center middle;
}
Grid {
grid-size: 2;
height: 36;
}
#primary { background: $primary }
#secondary { background: $secondary }
#background { background: $background }
#primary-background { background: $primary-background }
#secondary-background { background: $secondary-background }
#surface { background: $surface }
#panel { background: $panel }
#boost { background: $boost }
#warning { background: $warning }
#error {background: $error }
#success {background: $success }
#accent {background: $accent }
"""
def compose(self) -> ComposeResult:
with Grid():
for color in COLOR_NAMES:
yield Static(f"${color}", id=f"{color}")
yield Static(f"${color}", id=f"{color}-test")
def on_mount(self) -> None:
css_variables = self.get_css_variables()
for color in COLOR_NAMES:
self.query_one(f"#{color}-test").styles.background = css_variables[color]
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
The problem is that the
background
style expects a<color>
format, which doesn't include Textual's CSS variables.I don't know if this is the "proper" way, but using the
App.get_css_variables
method seems to work for most Textual colors, apart from the$boost
due to the alpha channel.