mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
added save_theme to ThemeManager
This commit is contained in:
parent
01e64f38e8
commit
81e979b567
@ -1,24 +1,29 @@
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from typing import List, Union
|
||||
|
||||
|
||||
class ThemeManager:
|
||||
|
||||
theme = {} # contains all the theme data
|
||||
built_in_themes = ["blue", "green", "dark-blue", "sweetkind"]
|
||||
theme: dict = {} # contains all the theme data
|
||||
_built_in_themes: List[str] = ["blue", "green", "dark-blue", "sweetkind"]
|
||||
_currently_loaded_theme: Union[str, None] = None
|
||||
|
||||
@classmethod
|
||||
def load_theme(cls, theme_name_or_path: str):
|
||||
script_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
if theme_name_or_path in cls.built_in_themes:
|
||||
if theme_name_or_path in cls._built_in_themes:
|
||||
with open(os.path.join(script_directory, "../../../assets", "themes", f"{theme_name_or_path}.json"), "r") as f:
|
||||
cls.theme = json.load(f)
|
||||
else:
|
||||
with open(theme_name_or_path, "r") as f:
|
||||
cls.theme = json.load(f)
|
||||
|
||||
# store theme path for saving
|
||||
cls._currently_loaded_theme = theme_name_or_path
|
||||
|
||||
# filter theme values for platform
|
||||
for key in cls.theme.keys():
|
||||
# check if values for key differ on platforms
|
||||
@ -29,3 +34,14 @@ class ThemeManager:
|
||||
cls.theme[key] = cls.theme[key]["Windows"]
|
||||
else:
|
||||
cls.theme[key] = cls.theme[key]["Linux"]
|
||||
|
||||
@classmethod
|
||||
def save_theme(cls):
|
||||
if cls._currently_loaded_theme is not None:
|
||||
if cls._currently_loaded_theme not in cls._built_in_themes:
|
||||
with open(cls._currently_loaded_theme, "r") as f:
|
||||
json.dump(cls.theme, f, indent=2)
|
||||
else:
|
||||
raise ValueError(f"cannot modify builtin theme '{cls._currently_loaded_theme}'")
|
||||
else:
|
||||
raise ValueError(f"cannot save theme, no theme is loaded")
|
||||
|
Loading…
Reference in New Issue
Block a user