hide window while titlebar changes on Windows

This commit is contained in:
Tom Schimansky 2022-02-18 14:33:45 +01:00
parent dd5601ce64
commit 7b32be8c76

View File

@ -31,17 +31,31 @@ class CTk(tkinter.Tk):
AppearanceModeTracker.add(self.set_appearance_mode, self) AppearanceModeTracker.add(self.set_appearance_mode, self)
super().configure(bg=CTkColorManager.single_color(self.fg_color, self.appearance_mode)) super().configure(bg=CTkColorManager.single_color(self.fg_color, self.appearance_mode))
self.window_exists = False # indicates if the window is already shown through .update or .mainloop
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
if self.appearance_mode == 1: if self.appearance_mode == 1:
self.windows_set_titlebar_color(self, "dark") self.windows_set_titlebar_color("dark")
else: else:
self.windows_set_titlebar_color(self, "light") self.windows_set_titlebar_color("light")
def destroy(self): def destroy(self):
AppearanceModeTracker.remove(self.set_appearance_mode) AppearanceModeTracker.remove(self.set_appearance_mode)
self.disable_macos_dark_title_bar() self.disable_macos_dark_title_bar()
super().destroy() super().destroy()
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)
def config(self, *args, **kwargs): def config(self, *args, **kwargs):
self.configure(*args, **kwargs) self.configure(*args, **kwargs)
@ -102,8 +116,7 @@ class CTk(tkinter.Tk):
os.system("defaults delete -g NSRequiresAquaSystemAppearance") os.system("defaults delete -g NSRequiresAquaSystemAppearance")
# This command reverts the dark-mode setting for all programs. # This command reverts the dark-mode setting for all programs.
@staticmethod def windows_set_titlebar_color(self, color_mode: str):
def windows_set_titlebar_color(window, color_mode: str):
""" """
Set the titlebar color of the window to light or dark theme on Microsoft Windows. Set the titlebar color of the window to light or dark theme on Microsoft Windows.
@ -114,7 +127,10 @@ class CTk(tkinter.Tk):
https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
""" """
window.update() if self.window_exists is False:
super().withdraw() # hide window if it not already exists to not show the .update call which is needed
super().update()
if color_mode.lower() == "dark": if color_mode.lower() == "dark":
DWMWA_USE_IMMERSIVE_DARK_MODE = 20 DWMWA_USE_IMMERSIVE_DARK_MODE = 20
@ -125,7 +141,7 @@ class CTk(tkinter.Tk):
set_window_attribute = ctypes.windll.dwmapi.DwmSetWindowAttribute set_window_attribute = ctypes.windll.dwmapi.DwmSetWindowAttribute
get_parent = ctypes.windll.user32.GetParent get_parent = ctypes.windll.user32.GetParent
hwnd = get_parent(window.winfo_id()) hwnd = get_parent(self.winfo_id())
rendering_policy = DWMWA_USE_IMMERSIVE_DARK_MODE rendering_policy = DWMWA_USE_IMMERSIVE_DARK_MODE
value = 2 value = 2
value = ctypes.c_int(value) value = ctypes.c_int(value)
@ -140,8 +156,8 @@ class CTk(tkinter.Tk):
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
if self.appearance_mode == 1: if self.appearance_mode == 1:
self.windows_set_titlebar_color(self, "dark") self.windows_set_titlebar_color("dark")
else: else:
self.windows_set_titlebar_color(self, "light") self.windows_set_titlebar_color("light")
super().configure(bg=CTkColorManager.single_color(self.fg_color, self.appearance_mode)) super().configure(bg=CTkColorManager.single_color(self.fg_color, self.appearance_mode))