import platform import sys import sv_ttk from ctypes import byref, c_int, sizeof, windll from distutils.version import StrictVersion as Version from os import system from tkinter import Tcl, Toplevel from typing import Literal, Union from ttkthemes import ThemedTk def theme_title_bar(window: Union[ThemedTk, Toplevel], mode: Literal["dark", "light"]) -> None: """ MORE INFO: https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute """ if mode == "dark": value = 1 #window.configure(background="#1c1c1c") elif mode == "light": value = 0 #window.configure(background="#ffffff") else: raise ValueError() try: sv_ttk.set_theme(mode) window.update() if sys.platform.startswith("win"): hwnd = windll.user32.GetParent(window.winfo_id()) DWMWA_USE_IMMERSIVE_DARK_MODE = 20 DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19 # try with DWMWA_USE_IMMERSIVE_DARK_MODE if windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, byref(c_int(value)), sizeof(c_int(value))) != 0: # try with DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20h1 windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, byref(c_int(value)), sizeof(c_int(value))) elif sys.platform == "darwin": if value == 1: if Version(platform.python_version()) < Version("3.10"): if Version(Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9 system("defaults write -g NSRequiresAquaSystemAppearance -bool No") # This command allows dark-mode for all programs else: if Version(platform.python_version()) < Version("3.10"): if Version(Tcl().call("info", "patchlevel")) >= Version("8.6.9"): # Tcl/Tk >= 8.6.9 system("defaults delete -g NSRequiresAquaSystemAppearance") # This command reverts the dark-mode setting for all programs. except Exception as err: print(err)