2021-03-04 20:27:46 +03:00
|
|
|
import sys
|
2022-01-07 22:54:45 +03:00
|
|
|
import tkinter
|
2021-07-13 12:23:34 +03:00
|
|
|
from distutils.version import StrictVersion as Version
|
2022-05-17 22:44:59 +03:00
|
|
|
from typing import Callable
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
try:
|
|
|
|
import darkdetect
|
|
|
|
|
|
|
|
if Version(darkdetect.__version__) < Version("0.3.1"):
|
2022-05-22 00:41:29 +03:00
|
|
|
sys.stderr.write("WARNING: You have to upgrade the darkdetect library: pip3 install --upgrade darkdetect\n")
|
2022-05-02 00:29:14 +03:00
|
|
|
if sys.platform != "darwin":
|
|
|
|
exit()
|
2022-05-21 01:25:07 +03:00
|
|
|
except ImportError as err:
|
|
|
|
raise err
|
|
|
|
except Exception:
|
|
|
|
sys.stderr.write("customtkinter.appearance_mode_tracker warning: failed to import darkdetect")
|
2021-03-06 15:47:49 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-22 16:36:36 +03:00
|
|
|
class AppearanceModeTracker:
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
callback_list = []
|
2022-05-22 21:26:31 +03:00
|
|
|
app_list = []
|
2022-01-07 22:54:45 +03:00
|
|
|
update_loop_running = False
|
2022-10-29 22:56:00 +03:00
|
|
|
update_loop_interval = 30 # milliseconds
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
appearance_mode_set_by = "system"
|
|
|
|
appearance_mode = 0 # Light (standard)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
@classmethod
|
|
|
|
def init_appearance_mode(cls):
|
|
|
|
if cls.appearance_mode_set_by == "system":
|
|
|
|
new_appearance_mode = cls.detect_appearance_mode()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if new_appearance_mode != cls.appearance_mode:
|
|
|
|
cls.appearance_mode = new_appearance_mode
|
|
|
|
cls.update_callbacks()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def add(cls, callback: Callable, widget=None):
|
2022-01-07 22:54:45 +03:00
|
|
|
cls.callback_list.append(callback)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if widget is not None:
|
2022-05-22 21:26:31 +03:00
|
|
|
app = cls.get_tk_root_of_widget(widget)
|
|
|
|
if app not in cls.app_list:
|
|
|
|
cls.app_list.append(app)
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if not cls.update_loop_running:
|
2022-08-19 01:13:00 +03:00
|
|
|
app.after(cls.update_loop_interval, cls.update)
|
2022-01-07 22:54:45 +03:00
|
|
|
cls.update_loop_running = True
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-02-23 00:38:40 +03:00
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def remove(cls, callback: Callable):
|
2022-06-28 12:16:28 +03:00
|
|
|
try:
|
|
|
|
cls.callback_list.remove(callback)
|
|
|
|
except ValueError:
|
|
|
|
return
|
2022-02-23 00:38:40 +03:00
|
|
|
|
2021-03-04 20:27:46 +03:00
|
|
|
@staticmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def detect_appearance_mode() -> int:
|
2021-07-13 12:23:34 +03:00
|
|
|
try:
|
|
|
|
if darkdetect.theme() == "Dark":
|
|
|
|
return 1 # Dark
|
|
|
|
else:
|
2021-03-04 20:27:46 +03:00
|
|
|
return 0 # Light
|
2021-07-13 12:23:34 +03:00
|
|
|
except NameError:
|
2021-03-04 20:27:46 +03:00
|
|
|
return 0 # Light
|
|
|
|
|
2022-02-23 00:38:40 +03:00
|
|
|
@classmethod
|
|
|
|
def get_tk_root_of_widget(cls, widget):
|
|
|
|
current_widget = widget
|
|
|
|
|
|
|
|
while isinstance(current_widget, tkinter.Tk) is False:
|
|
|
|
current_widget = current_widget.master
|
|
|
|
|
|
|
|
return current_widget
|
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
@classmethod
|
|
|
|
def update_callbacks(cls):
|
|
|
|
if cls.appearance_mode == 0:
|
|
|
|
for callback in cls.callback_list:
|
|
|
|
try:
|
|
|
|
callback("Light")
|
|
|
|
except Exception:
|
|
|
|
continue
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
elif cls.appearance_mode == 1:
|
|
|
|
for callback in cls.callback_list:
|
|
|
|
try:
|
|
|
|
callback("Dark")
|
|
|
|
except Exception:
|
|
|
|
continue
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
@classmethod
|
|
|
|
def update(cls):
|
|
|
|
if cls.appearance_mode_set_by == "system":
|
|
|
|
new_appearance_mode = cls.detect_appearance_mode()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if new_appearance_mode != cls.appearance_mode:
|
|
|
|
cls.appearance_mode = new_appearance_mode
|
|
|
|
cls.update_callbacks()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
# find an existing tkinter.Tk object for the next call of .after()
|
2022-05-22 21:26:31 +03:00
|
|
|
for app in cls.app_list:
|
2022-01-07 22:54:45 +03:00
|
|
|
try:
|
2022-05-22 21:26:31 +03:00
|
|
|
app.after(cls.update_loop_interval, cls.update)
|
2022-01-07 22:54:45 +03:00
|
|
|
return
|
|
|
|
except Exception:
|
|
|
|
continue
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
cls.update_loop_running = False
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def get_mode(cls) -> int:
|
2021-03-04 20:27:46 +03:00
|
|
|
return cls.appearance_mode
|
|
|
|
|
|
|
|
@classmethod
|
2022-05-17 22:44:59 +03:00
|
|
|
def set_appearance_mode(cls, mode_string: str):
|
2021-03-04 20:27:46 +03:00
|
|
|
if mode_string.lower() == "dark":
|
2022-01-07 22:54:45 +03:00
|
|
|
cls.appearance_mode_set_by = "user"
|
|
|
|
new_appearance_mode = 1
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if new_appearance_mode != cls.appearance_mode:
|
|
|
|
cls.appearance_mode = new_appearance_mode
|
|
|
|
cls.update_callbacks()
|
2021-03-04 20:27:46 +03:00
|
|
|
|
|
|
|
elif mode_string.lower() == "light":
|
2022-01-07 22:54:45 +03:00
|
|
|
cls.appearance_mode_set_by = "user"
|
|
|
|
new_appearance_mode = 0
|
2021-03-04 20:27:46 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
if new_appearance_mode != cls.appearance_mode:
|
|
|
|
cls.appearance_mode = new_appearance_mode
|
|
|
|
cls.update_callbacks()
|
2022-01-02 00:16:06 +03:00
|
|
|
|
2022-01-07 22:54:45 +03:00
|
|
|
elif mode_string.lower() == "system":
|
|
|
|
cls.appearance_mode_set_by = "system"
|