Programmatic access to colors in the current theme #5406
-
Is there a way to get programmatic access to the colors used by the current theme? I want to be able to set the color of a Widget or a RichRenderable based on the current them, for example, to "$accent". The app has access to a What's the right approach to this? Thanks Andy |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Perhaps the answer involves Widget.get_component_styles() and Widget.get_component_rich_style()? These would not return the current theme directly, but (I think?) they return a programmatic view of a CSS object which could be defined in terms of the current theme. |
Beta Was this translation helpful? Give feedback.
-
I think you might be looking for the from textual.app import App, ComposeResult
from textual.widgets import Label
class ThemeVariablesApp(App):
def compose(self) -> ComposeResult:
yield Label("$accent = ??????")
def on_mount(self) -> None:
label = self.query_one(Label)
accent_color = self.theme_variables["accent"]
label.update(f"$accent = {accent_color}")
label.styles.color = accent_color
if __name__ == "__main__":
app = ThemeVariablesApp()
app.run() |
Beta Was this translation helpful? Give feedback.
You can simply use the
app
property which returns the current app - i.e.self.app.theme_variables