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))
@Olikonsti
Copy link
Author

Screenshot 2022-01-15 181409

@coder628
Copy link

Nice

@taranjeet13
Copy link

I'm on windows 10 and it's not working properly.….I mean when I
run my code….it doesn't show any changes...but when I minimize
my window and maximize it again..only then it shows me the
dark title bar
Is there any solution to that?
Well it sure is looking nice but well….not working properly 😅

@Olikonsti
Copy link
Author

Olikonsti commented Feb 20, 2022

I think it only works properly on windows 11. i did not find a solution for windows 10. but you could try to iconify() and deiconify() the window at startup

@IdkWhatToCallMe123
Copy link

Just changing the window size worked on win10 for me.

#Changes the window size
root.geometry(str(root.winfo_width()+1) + "x" + str(root.winfo_height()+1))
#Returns to original size
root.geometry(str(root.winfo_width()-1) + "x" + str(root.winfo_height()-1))

image

@HuyHung1408
Copy link

2022-03-26-201634.mp4

Maybe a stupid question but how can I set the title bar back to white? :)

@tomlin7
Copy link

tomlin7 commented May 7, 2022

@HuyHung1408 pass 0 as the value, that will do it.

def light_title_bar(window):
    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 = 0
    value = ct.c_int(value)
    set_window_attribute(hwnd, rendering_policy, ct.byref(value), ct.sizeof(value))

@HuyHung1408
Copy link

Thanks!

@arthurdeka
Copy link

arthurdeka commented Jun 14, 2022

Just changing the window size worked on win10 for me.

#Changes the window size
root.geometry(str(root.winfo_width()+1) + "x" + str(root.winfo_height()+1))
#Returns to original size
root.geometry(str(root.winfo_width()-1) + "x" + str(root.winfo_height()-1))

worked for me, thanks dude!

@SarthakTools
Copy link

SarthakTools commented Dec 29, 2022

This works properly .................................

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()

@shakealeg
Copy link

Didn't work on Windows 11, title bar is same color as before.

@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