Replies: 3 comments 4 replies
-
|
To answer the question in the subject: no, there's no direct method in Textual for "unloading" a CSS file (and also keep in mind that styling of a widget can happen in more places than just a CSS file). More generally though, if you wished to have whole themes defined, an approach would be to apply a class to from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Label, Select, Rule
class ExampleThemeApp(App[None]):
CSS = """
Label {
border: solid white;
width: 1fr;
text-align: center;
}
App.theme-reds Label {
border: solid red;
background: red 20%;
color: red;
}
App.theme-greens Label {
border: solid green;
background: green 20%;
color: green;
}
App.theme-blues Label {
border: solid blue;
background: blue 20%;
color: blue;
}
"""
THEMES = ("reds", "greens", "blues")
def compose(self) -> ComposeResult:
yield Select(((theme.capitalize(), theme) for theme in self.THEMES))
yield Rule()
for n in range(20):
yield Label(f"This is example label {n}")
@on(Select.Changed)
def change_theme(self, event: Select.Changed) -> None:
for theme in self.THEMES:
self.set_class(event.value == theme, f"theme-{theme}")
if __name__ == "__main__":
ExampleThemeApp().run() |
Beta Was this translation helpful? Give feedback.
-
|
Yes, this is the approach I took initially and it works! Also, You have set the class directly in the app, which seems a better idea. What I did was apply the class to all the widgets which didn't turn out to be a good option since unmounted widgets won't have the theme applied when mounted (this was also a reason to open this discussion) |
Beta Was this translation helpful? Give feedback.
-
|
Hey @davep! Ok, I have one more question, kinda related to this thread. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to make a theme changer and I'm currently using classes (like
.theme-name) to override the color/styles in a widget but is there a way to dynamically load/unload css files?I looked into the
action_toggle_darkwhich also uses classnames.Textual is able to determine any file changes in the CSS file, but It'd be great if I can change the
CSS_PATHon demand :DBeta Was this translation helpful? Give feedback.
All reactions