Sun Valley ttk theme

repository·main·Indexed 25 days ago

https://github.com/rdbende/sun-valley-ttk-theme

A Python package providing a modern theme for Tkinter applications using the ttk widget set. Supports light and dark modes via sv_ttk.set_theme() and offers integration with darkdetect for system color scheme matching and pywinstyles for Windows title bar customization.

Tokens
769
Snippets
4
Records
4
Agent score
33%

What's inside sv-ttk

  1. Enable dark mode title bar on Windows

    main

    By 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)
  2. Apply the Sun Valley theme to Tkinter

    main

    To use the theme, import sv_ttk and call sv_ttk.set_theme() with either 'dark' or 'light'.

    Note: The theme only applies to tkinter.ttk widgets. Regular tkinter widgets will only inherit the colorscheme but will not receive the full themed styling.

    import tkinter
    from tkinter import ttk
    import sv_ttk
    
    root = tkinter.Tk()
    
    button = ttk.Button(root, text="Click me!")
    button.pack()
    
    # Apply the theme
    sv_ttk.set_theme("dark")
    
    root.mainloop()
  3. Set the theme to match the system color scheme

    main

    You can automatically switch between light and dark modes by using the darkdetect package. Pass the result of darkdetect.theme() directly into sv_ttk.set_theme().

    import darkdetect
    import sv_ttk
    
    sv_ttk.set_theme(darkdetect.theme())