theme¶
classDiagram
CharmyObject <|-- Theme
- class charmy.styles.theme.Theme(styles: dict | None = None, parent: Theme | None = None, *kwargs)¶
基类:
CharmyObjectTheme class for CWindow and CWidgets.
Example¶
my_theme = CTheme({<Some styles>}) my_sub_theme = CTheme(parent="default.light") my_external_theme = CTheme().load_from_file("./path/to/a/theme.json")
This shows examples of creating themes, either from a JSON, a parent theme or a file.
all_themes = CTheme.LOADED_THEMES INTERNAL_THEME = CTheme.INTERNAL_THEME DEFAULT_THEME = CTheme.DEFAULT_THEME
This shows getting all loaded themes, internal themes, and the default theme.
default_light_theme = CTheme.find_loaded_theme("default.light") if CTheme.validate_theme_existed("default.light"): print("Default light theme exists!")
This shows finding a theme and checking if it exists
重置后我摸改了一些地方,但逻辑、功能应是大差不差的,某些方法名、变量名我改的易读了点
- INTERNAL_THEME_DIR = PosixPath('/home/runner/work/charmy-docs/charmy-docs/charmy/charmy/resources/themes')¶
- DEFAULT_THEME_FILENAME: str = 'light'¶
- EXPECTED_DATA_TYPE = {'base': <class 'str'>, 'color_palette': <class 'dict'>, 'friendly_name': <class 'str'>, 'name': <class 'str'>, 'styles': <class 'dict'>}¶
- __init__(styles: dict | None = None, parent: Theme | None = None, *kwargs) None¶
Theme for CWindow and CWidgets.
Example¶
my_theme = CTheme({<Some styles>}) my_sub_theme = CTheme(parent="default.light") my_external_theme = CTheme().load_from_file("./path/to/a/theme.json")
This shows examples of creating themes, either from a JSON, a parent theme or a file.
- param styles:
Styles of the theme
- param parent:
Parent theme
- classmethod find_loaded_theme(theme_name: str) Theme | Literal[False]¶
Search for a loaded theme by name, returns the CTheme object if found, or False if not.
Example¶
DEFAULT_THEME = CTheme.find_loaded_theme("default.light")
This returns the CTheme object of the default theme to DEFAULT_THEME.
- param theme_name:
Name of the theme to load
- return:
The CTheme object if found, otherwise False
- classmethod validate_theme_existed(theme_name: str) bool¶
Validate if the theme with given name existed and loaded.
Example¶
CTheme.validate_theme_existed("default.light")
This returns if the theme default.light is loaded.
- param theme_name:
Name of the theme to validate
- return:
If the theme loaded
- load_from_file(file_path: str | Path) Theme | Literal[False]¶
Load styles to theme from a file.
Example¶
my_theme = CTheme().load_from_file("./path/to/a/theme.json") my_theme.load_from_file("./path/to/another/theme.json")
This shows loading a theme to my_theme from the theme file at ./path/to/a/theme.json, and change it to theme from ./path/to/another/theme.json later.
- param file_path:
Path to the theme file
- return self:
The CTheme itself
- load_from_json(theme_data: dict) Theme¶
Load all data (including metadata) to the theme.
Example¶
my_theme = CTheme().load_from_json({<Some JSON theme data>}) my_theme.load_from_json({<Some JSON theme data>})
This shows loading a theme to my_theme from JSON data, and change it to theme from another JSON later.
- param theme_data:
dict that contains the theme data
- return self:
The CTheme itself