2021-12-31 21:07:53 +03:00
|
|
|
import tkinter
|
|
|
|
from distutils.version import StrictVersion as Version
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import platform
|
2022-02-17 18:39:39 +03:00
|
|
|
import ctypes
|
2021-12-31 21:07:53 +03:00
|
|
|
|
|
|
|
from .appearance_mode_tracker import AppearanceModeTracker
|
2022-02-23 00:38:40 +03:00
|
|
|
from .customtkinter_theme_manager import CTkThemeManager
|
2021-12-31 21:07:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTk(tkinter.Tk):
|
|
|
|
def __init__(self, *args,
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
2021-12-31 21:07:53 +03:00
|
|
|
**kwargs):
|
|
|
|
|
|
|
|
self.enable_macos_dark_title_bar()
|
2022-02-21 17:35:08 +03:00
|
|
|
super().__init__(*args, **kwargs)
|
2021-12-31 21:07:53 +03:00
|
|
|
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
|
|
|
|
2022-03-01 18:17:39 +03:00
|
|
|
self.fg_color = CTkThemeManager.theme["color"]["window_bg_color"] if fg_color == "default_theme" else fg_color
|
2022-01-08 02:29:53 +03:00
|
|
|
|
2021-12-31 21:07:53 +03:00
|
|
|
if "bg" in kwargs:
|
2022-01-07 22:54:45 +03:00
|
|
|
self.fg_color = kwargs["bg"]
|
2021-12-31 21:07:53 +03:00
|
|
|
del kwargs["bg"]
|
|
|
|
elif "background" in kwargs:
|
2022-01-07 22:54:45 +03:00
|
|
|
self.fg_color = kwargs["background"]
|
2021-12-31 21:07:53 +03:00
|
|
|
del kwargs["background"]
|
|
|
|
|
2022-04-07 00:09:54 +03:00
|
|
|
# add set_appearance_mode method to callback list of AppearanceModeTracker for appearance mode changes
|
2022-01-07 22:54:45 +03:00
|
|
|
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
2022-02-23 00:38:40 +03:00
|
|
|
super().configure(bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
|
|
|
super().title("CTk")
|
2021-12-31 21:07:53 +03:00
|
|
|
|
2022-02-18 16:33:45 +03:00
|
|
|
self.window_exists = False # indicates if the window is already shown through .update or .mainloop
|
|
|
|
|
2022-02-17 18:39:39 +03:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
if self.appearance_mode == 1:
|
2022-02-18 16:33:45 +03:00
|
|
|
self.windows_set_titlebar_color("dark")
|
2022-02-17 18:39:39 +03:00
|
|
|
else:
|
2022-02-18 16:33:45 +03:00
|
|
|
self.windows_set_titlebar_color("light")
|
2022-02-17 18:39:39 +03:00
|
|
|
|
2021-12-31 21:07:53 +03:00
|
|
|
def destroy(self):
|
2022-01-02 00:16:06 +03:00
|
|
|
AppearanceModeTracker.remove(self.set_appearance_mode)
|
2021-12-31 21:07:53 +03:00
|
|
|
self.disable_macos_dark_title_bar()
|
|
|
|
super().destroy()
|
|
|
|
|
2022-02-18 16:33:45 +03:00
|
|
|
def update(self):
|
|
|
|
if self.window_exists is False:
|
|
|
|
self.deiconify()
|
|
|
|
self.window_exists = True
|
|
|
|
super().update()
|
|
|
|
|
|
|
|
def mainloop(self, *args, **kwargs):
|
|
|
|
if self.window_exists is False:
|
|
|
|
self.deiconify()
|
|
|
|
self.window_exists = True
|
|
|
|
super().mainloop(*args, **kwargs)
|
|
|
|
|
2022-02-21 17:35:08 +03:00
|
|
|
def resizable(self, *args, **kwargs):
|
|
|
|
super().resizable(*args, **kwargs)
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
if self.appearance_mode == 1:
|
|
|
|
self.windows_set_titlebar_color("dark")
|
|
|
|
else:
|
|
|
|
self.windows_set_titlebar_color("light")
|
|
|
|
|
2021-12-31 21:07:53 +03:00
|
|
|
def config(self, *args, **kwargs):
|
|
|
|
self.configure(*args, **kwargs)
|
|
|
|
|
|
|
|
def configure(self, *args, **kwargs):
|
2022-01-07 22:54:45 +03:00
|
|
|
bg_changed = False
|
|
|
|
|
2021-12-31 21:07:53 +03:00
|
|
|
if "bg" in kwargs:
|
2022-01-07 22:54:45 +03:00
|
|
|
self.fg_color = kwargs["bg"]
|
|
|
|
bg_changed = True
|
2022-02-23 00:38:40 +03:00
|
|
|
kwargs["bg"] = CTkThemeManager.single_color(self.fg_color, self.appearance_mode)
|
2021-12-31 21:07:53 +03:00
|
|
|
elif "background" in kwargs:
|
2022-01-07 22:54:45 +03:00
|
|
|
self.fg_color = kwargs["background"]
|
|
|
|
bg_changed = True
|
2022-02-23 00:38:40 +03:00
|
|
|
kwargs["background"] = CTkThemeManager.single_color(self.fg_color, self.appearance_mode)
|
2022-01-07 22:54:45 +03:00
|
|
|
elif "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
2022-02-23 00:38:40 +03:00
|
|
|
kwargs["bg"] = CTkThemeManager.single_color(self.fg_color, self.appearance_mode)
|
2022-01-07 22:54:45 +03:00
|
|
|
del kwargs["fg_color"]
|
|
|
|
bg_changed = True
|
|
|
|
|
|
|
|
elif len(args) > 0 and type(args[0]) == dict:
|
|
|
|
if "bg" in args[0]:
|
|
|
|
self.fg_color=args[0]["bg"]
|
|
|
|
bg_changed = True
|
2022-02-23 00:38:40 +03:00
|
|
|
args[0]["bg"] = CTkThemeManager.single_color(self.fg_color, self.appearance_mode)
|
2022-01-07 22:54:45 +03:00
|
|
|
elif "background" in args[0]:
|
|
|
|
self.fg_color=args[0]["background"]
|
|
|
|
bg_changed = True
|
2022-02-23 00:38:40 +03:00
|
|
|
args[0]["background"] = CTkThemeManager.single_color(self.fg_color, self.appearance_mode)
|
2022-01-07 22:54:45 +03:00
|
|
|
|
|
|
|
if bg_changed:
|
|
|
|
from .customtkinter_slider import CTkSlider
|
|
|
|
from .customtkinter_progressbar import CTkProgressBar
|
|
|
|
from .customtkinter_label import CTkLabel
|
|
|
|
from .customtkinter_frame import CTkFrame
|
|
|
|
from .customtkinter_entry import CTkEntry
|
2022-02-28 02:35:52 +03:00
|
|
|
from customtkinter.customtkinter_checkbox import CTkCheckBox
|
2022-01-07 22:54:45 +03:00
|
|
|
from .customtkinter_button import CTkButton
|
|
|
|
|
|
|
|
for child in self.winfo_children():
|
|
|
|
if isinstance(child, (CTkFrame, CTkButton, CTkLabel, CTkSlider, CTkCheckBox, CTkEntry, CTkProgressBar)):
|
|
|
|
child.configure(bg_color=self.fg_color)
|
2021-12-31 21:07:53 +03:00
|
|
|
|
|
|
|
super().configure(*args, **kwargs)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def enable_macos_dark_title_bar():
|
|
|
|
if sys.platform == "darwin": # macOS
|
|
|
|
if Version(platform.python_version()) < Version("3.10"):
|
|
|
|
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
|
|
|
os.system("defaults write -g NSRequiresAquaSystemAppearance -bool No")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def disable_macos_dark_title_bar():
|
|
|
|
if sys.platform == "darwin": # macOS
|
|
|
|
if Version(platform.python_version()) < Version("3.10"):
|
|
|
|
if Version(tkinter.Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9
|
|
|
|
os.system("defaults delete -g NSRequiresAquaSystemAppearance")
|
|
|
|
# This command reverts the dark-mode setting for all programs.
|
|
|
|
|
2022-02-18 16:33:45 +03:00
|
|
|
def windows_set_titlebar_color(self, color_mode: str):
|
2022-02-17 18:39:39 +03:00
|
|
|
"""
|
|
|
|
Set the titlebar color of the window to light or dark theme on Microsoft Windows.
|
|
|
|
|
|
|
|
Credits for this function:
|
|
|
|
https://stackoverflow.com/questions/23836000/can-i-change-the-title-bar-in-tkinter/70724666#70724666
|
|
|
|
|
|
|
|
MORE INFO:
|
|
|
|
https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
|
|
|
|
"""
|
|
|
|
|
2022-02-21 17:35:08 +03:00
|
|
|
if sys.platform.startswith("win"):
|
2022-02-18 16:33:45 +03:00
|
|
|
|
2022-02-21 17:35:08 +03:00
|
|
|
super().withdraw() # hide window so that it can be redrawn after the titlebar change so that the color change is visible
|
|
|
|
if not self.window_exists:
|
|
|
|
super().update()
|
|
|
|
|
|
|
|
if color_mode.lower() == "dark":
|
|
|
|
value = 1
|
|
|
|
elif color_mode.lower() == "light":
|
|
|
|
value = 0
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
hwnd = ctypes.windll.user32.GetParent(self.winfo_id())
|
|
|
|
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
|
|
|
|
DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19
|
|
|
|
|
|
|
|
# try with DWMWA_USE_IMMERSIVE_DARK_MODE
|
|
|
|
if ctypes.windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
|
|
|
|
ctypes.byref(ctypes.c_int(value)),
|
|
|
|
ctypes.sizeof(ctypes.c_int(value))) != 0:
|
|
|
|
|
|
|
|
# try with DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20h1
|
|
|
|
ctypes.windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
|
|
|
|
ctypes.byref(ctypes.c_int(value)),
|
|
|
|
ctypes.sizeof(ctypes.c_int(value)))
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
print(err)
|
2022-02-17 18:39:39 +03:00
|
|
|
|
2022-02-21 17:35:08 +03:00
|
|
|
if self.window_exists:
|
|
|
|
self.deiconify()
|
2022-02-17 18:39:39 +03:00
|
|
|
|
2021-12-31 21:07:53 +03:00
|
|
|
def set_appearance_mode(self, mode_string):
|
|
|
|
if mode_string.lower() == "dark":
|
|
|
|
self.appearance_mode = 1
|
|
|
|
elif mode_string.lower() == "light":
|
|
|
|
self.appearance_mode = 0
|
|
|
|
|
2022-02-17 18:39:39 +03:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
if self.appearance_mode == 1:
|
2022-02-18 16:33:45 +03:00
|
|
|
self.windows_set_titlebar_color("dark")
|
2022-02-17 18:39:39 +03:00
|
|
|
else:
|
2022-02-18 16:33:45 +03:00
|
|
|
self.windows_set_titlebar_color("light")
|
2022-02-17 18:39:39 +03:00
|
|
|
|
2022-02-23 00:38:40 +03:00
|
|
|
super().configure(bg=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|