ultimatevocalremovergui/gui_data/sv_ttk/__init__.py

70 lines
1.6 KiB
Python
Raw Normal View History

2022-12-19 04:27:09 +01:00
from pathlib import Path
2022-12-27 11:32:50 +01:00
import platform
if platform.system() == "Darwin":
sun_valley_tcl = "sun-valley_darwin.tcl"
else:
sun_valley_tcl = "sun-valley.tcl"
2022-12-19 04:27:09 +01:00
inited = False
root = None
def init(func):
def wrapper(*args, **kwargs):
global inited
global root
if not inited:
from tkinter import _default_root
2022-12-27 11:32:50 +01:00
path = (Path(__file__).parent / sun_valley_tcl).resolve()
2022-12-19 04:27:09 +01:00
try:
_default_root.tk.call("source", str(path))
except AttributeError:
raise RuntimeError(
"can't set theme. "
"Tk is not initialized. "
"Please first create a tkinter.Tk instance, then set the theme."
) from None
else:
inited = True
root = _default_root
return func(*args, **kwargs)
return wrapper
@init
2023-09-27 05:01:36 +02:00
def set_theme(theme, font_name="Century Gothic", f_size=10, fg_color_set="#F6F6F7"):
2022-12-19 04:27:09 +01:00
if theme not in {"dark", "light"}:
raise RuntimeError(f"not a valid theme name: {theme}")
2023-09-26 00:13:31 +02:00
root.globalsetvar("fontName", (font_name, f_size))
2023-09-27 05:01:36 +02:00
root.globalsetvar("fgcolorset", (fg_color_set))
2022-12-19 04:27:09 +01:00
root.tk.call("set_theme", theme)
@init
def get_theme():
theme = root.tk.call("ttk::style", "theme", "use")
try:
return {"sun-valley-dark": "dark", "sun-valley-light": "light"}[theme]
except KeyError:
return theme
@init
def toggle_theme():
if get_theme() == "dark":
use_light_theme()
else:
use_dark_theme()
use_dark_theme = lambda: set_theme("dark")
use_light_theme = lambda: set_theme("light")