TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.94k stars 1.03k forks source link

CTk window loses its themed title bar after exiting fullscreen #1043

Open legopitstop opened 1 year ago

legopitstop commented 1 year ago

CTk window loses its themed title bar when you exit fullscreen

customtkinter: 5.0.3 OS: Windows 10

Steps To Reproduce

  1. Run the below code in a python file.
  2. Press F11 to enter fullscreen
  3. Press F11 again to exit fullscreen
  4. The title bar is now white

Observed Results

Code:

import customtkinter

customtkinter.set_appearance_mode('dark')
class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.bind('<F11>', self.toggle_fullscreen)
    def toggle_fullscreen(self, e=None):
        if self.attributes('-fullscreen'): self.attributes('-fullscreen', False)
        else: self.attributes('-fullscreen', True)
app = App()
app.mainloop()
Akascape commented 1 year ago

@legopitstop Somehow, this is a bug. But there is a way to fix this. Add this code under toggle_fullscreen():

import ctypes
if customtkinter.get_appearance_mode()=="Dark":
    value=1
else:
    value=0
try:
    hwnd = ctypes.windll.user32.GetParent(self.winfo_id())
    DWMWA_USE_IMMERSIVE_DARK_MODE = 20
    DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19

    if ctypes.windll.dwmapi.DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE,
                                                  ctypes.byref(ctypes.c_int(value)),
                                                  ctypes.sizeof(ctypes.c_int(value))) != 0:

        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)

Other useful links: https://github.com/TomSchimansky/CustomTkinter/discussions/1011#discussioncomment-4626534