Skip to content

Instantly share code, notes, and snippets.

@Olikonsti
Last active March 23, 2024 23:41
Show Gist options
  • Save Olikonsti/879edbf69b801d8519bf25e804cec0aa to your computer and use it in GitHub Desktop.
Save Olikonsti/879edbf69b801d8519bf25e804cec0aa to your computer and use it in GitHub Desktop.
import ctypes as ct
def dark_title_bar(window):
"""
MORE INFO:
https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
"""
window.update()
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
set_window_attribute = ct.windll.dwmapi.DwmSetWindowAttribute
get_parent = ct.windll.user32.GetParent
hwnd = get_parent(window.winfo_id())
rendering_policy = DWMWA_USE_IMMERSIVE_DARK_MODE
value = 2
value = ct.c_int(value)
set_window_attribute(hwnd, rendering_policy, ct.byref(value), ct.sizeof(value))
@SarthakTools
Copy link

try it now...

@shakealeg
Copy link

shakealeg commented Feb 10, 2023

import ctypes as ct from tkinter import *

root = Tk() root.update() root.iconify()

DWWMA_USE_IMMERSIVE_DARK_MODE = 20 set_window_attribute = ct.windll.dwmapi.DwmSetWindowAttribute get_parent = ct.windll.user32.GetParent hwnd = get_parent(root.winfo_id()) renduring_policy = DWWMA_USE_IMMERSIVE_DARK_MODE value = 1 value = ct.c_int(value) set_window_attribute(hwnd, renduring_policy, ct.byref(value), ct.sizeof(value)) root.update_idletasks()

root.deiconify() root.mainloop()

Doesn't work.

@SarthakTools
Copy link

SarthakTools commented Feb 10, 2023

try this one if it doesn't work then minimize the window and maximize it if u r getting any error then pls tell me...

import ctypes as ct
from tkinter import *

root = Tk()
root.update()
root.geometry("600x400")
root.config(bg="#222")

DARK_MODE = 20
set_windows_attribute = ct.windll.dwmapi.DwmSetWindowAttribute
get_parent = ct.windll.user32.GetParent
hwnd = get_parent(root.winfo_id())
value = 2
value = ct.c_int(value)
renduring_policy = DARK_MODE
set_windows_attribute(hwnd, renduring_policy, ct.byref(value), ct.sizeof(value))

root.mainloop()

@shakealeg
Copy link

try this one if it doesn't work then minimize the window and maximize it if u r getting any error then pls tell me...

import ctypes as ct from tkinter import *

root = Tk() root.update() root.geometry("600x400") root.config(bg="#222")

DARK_MODE = 20 set_windows_attribute = ct.windll.dwmapi.DwmSetWindowAttribute get_parent = ct.windll.user32.GetParent hwnd = get_parent(root.winfo_id()) value = 2 value = ct.c_int(value) renduring_policy = DARK_MODE set_windows_attribute(hwnd, renduring_policy, ct.byref(value), ct.sizeof(value))

root.mainloop()

This worked, but how can I make it so its black all the time. Bypassing peoples custom accent color.

@SarthakTools
Copy link

Yes write after the geometry add this root.iconify()
root.update()

Then at last before the root.mainloop add this
root.deconify()

Then it will work properly...

@SarthakTools
Copy link

You can install the pywinstyles module for Windows 11 and go through it on pypy website and search this module "pywinstyles". You can customise the Windows very beautifully through it go to the project description thoroughly then u will understand after watching the pics...

@tomlin7
Copy link

tomlin7 commented Jul 1, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment