py-window-styles

repository·main·Indexed 20 days ago

https://github.com/akascape/py-window-styles

A Python library for customizing the visual appearance of UI windows on Windows. It provides access to Windows 11 header styles such as Mica and Acrylic, as well as custom coloring for title bars, title text, and borders. The library supports integration with Tkinter, CustomTkinter, PyGame, PyQt, PySide, WxPython, and PySimpleGUI, or any UI library via the window handle (HWND).

Tokens
1.6K
Snippets
9
Records
11
Agent score
70%

What's inside pywinstyles

  1. Apply window styles to unsupported UI libraries via HWND

    main

    For libraries not explicitly supported (like Kivy), you must retrieve the window handle (HWND) and pass it to pywinstyles.

    To get the active window handle:

    from ctypes import windll
    hwnd = windll.user32.GetActiveWindow()

    To find a window handle by its title:

    from ctypes import windll
    hwnd = windll.user32.FindWindowW(None, window_name)
    from ctypes import windll
    import pywinstyles
    
    # Get active window handle
    hwnd = windll.user32.GetActiveWindow()
    
    # Or find by name
    # hwnd = windll.user32.FindWindowW(None, "Window Name")
    
    pywinstyles.change_header_color(hwnd, color="blue")
  2. Apply window styles to PySimpleGUI

    main

    When using PySimpleGUI, you must access the underlying Tkinter root object via window.TKroot to pass it to pywinstyles. Ensure the window is finalized (using finalize=True) before applying styles.

    import PySimpleGUI as sg
    import pywinstyles
    
    layout = [[sg.Text("pywinstyles example")], [sg.Button("OK")]]
    window = sg.Window("Demo", layout, finalize=True)
    
    pywinstyles.change_header_color(window.TKroot, color="blue")
  3. Apply window styles to PyGame

    main

    PyGame requires you to manually retrieve the window handle (HWND) before passing it to pywinstyles. You can get the HWND using pygame.display.get_wm_info()["window"].

    import pygame
    import pywinstyles
    
    screen = pygame.display.set_mode((300, 200))
    hwnd = pygame.display.get_wm_info()["window"]
    pywinstyles.change_header_color(hwnd, color="blue")
  4. Apply window styles to PyQt and PySide

    main

    For PyQt (e.g., PyQt5) or PySide (e.g., PySide2), pass the window object directly to pywinstyles.change_header_color.

    # PyQt5 Example
    import sys
    from PyQt5 import QtWidgets
    import pywinstyles
    
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    pywinstyles.change_header_color(window, color="blue")
    window.show()
  5. Apply window styles to Tkinter and CustomTkinter

    main

    To change the header color of a Tkinter or CustomTkinter window, pass the window instance (root) to pywinstyles.change_header_color and specify the color argument.

    import tkinter
    import pywinstyles
    
    root = tkinter.Tk()
    pywinstyles.change_header_color(root, color="blue")
    root.mainloop()
  6. Apply Custom Window Colors (Windows 11 only)

    main

    If you are running on Windows 11, you can customize specific window elements using the following methods:

    • Change Title Bar Color: pywinstyles.change_header_color(window, color="#HEX")
    • Change Title Text Color: pywinstyles.change_title_color(window, color="color_name_or_hex")
    • Change Border Color: pywinstyles.change_border_color(window, color="#HEX")
    • Get Windows Accent Color: pywinstyles.get_accent_color() returns the current system accent color as a hex string.
    import pywinstyles
    
    # Change title bar to a specific hex color
    pywinstyles.change_header_color(window, color="#00524d")
    
    # Change title text to white
    pywinstyles.change_title_color(window, color="white")
    
    # Change border color
    pywinstyles.change_border_color(window, color="#00ffff")
    
    # Retrieve system accent color
    accent = pywinstyles.get_accent_color()
  7. Apply Window Styles and Themes

    main

    Use pywinstyles.apply_style(window, style) to apply built-in Windows 11 header styles or Windows 10 themes to your window.

    Supported Styles:

    • mica: Windows 11 Mica effect.
    • acrylic: Windows 11 Acrylic effect.
    • aero: Aero effect (GPU Heavy).
    • transparent: Transparent effect (GPU Heavy).
    • optimised: Optimized style.
    • win7: Windows 7 style.
    • inverse: Inverse style.
    • native: Native window style.
    • popup: Popup style.
    • dark: Dark mode style.
    • normal: Resets to no change.
    import pywinstyles
    
    # window is your UI library's window object (Tkinter, PyQt, etc.)
    pywinstyles.apply_style(window, 'mica')
  8. Set widget opacity and transparency

    main

    You can control the visibility of individual widgets using pywinstyles.set_opacity.

    • Set overall opacity: Pass a float value between 0.0 and 1.0 to value.
    • Make a specific color transparent: Pass a color string (e.g., `