Enable dark mode title bar on Windows
mainBy default, the Sun Valley theme does not change the Windows title bar color. To achieve a dark title bar, use the pywinstyles library.
Platform Support:
- Windows 11: Allows setting the title bar to any color (e.g., matching the background).
- Windows 10: Limited to black for dark mode and white for light mode. A workaround using
wm_attributes("-alpha", ...)is required to force an immediate update.
Warning: This functionality is Windows-only. Ensure you check the platform before calling window-style functions.
import pywinstyles, sys
import sv_ttk
def apply_theme_to_titlebar(root):
version = sys.getwindowsversion()
if version.major == 10 and version.build >= 22000:
# Windows 11: Set title bar color to match background
pywinstyles.change_header_color(root, "#1c1c1c" if sv_ttk.get_theme() == "dark" else "#fafafa")
elif version.major == 10:
# Windows 10: Apply style and use alpha hack to refresh
pywinstyles.apply_style(root, "dark" if sv_ttk.get_theme() == "dark" else "normal")
root.wm_attributes("-alpha", 0.99)
root.wm_attributes("-alpha", 1)
# Usage with your Toplevel/Tk root
apply_theme_to_titlebar(root)