moses-palmer / pystray

GNU General Public License v3.0
479 stars 59 forks source link

Color customization or Dark Theme #130

Closed MaxBQb closed 1 year ago

MaxBQb commented 2 years ago

Discord/Telegram already have custom colored tray menus Is there way to doing so with pystray?

Can you add this options?

MaxBQb commented 2 years ago

I find out strange thing. Simple tray menu used: image

When I call tkinter dialog (windows explorer) with dark theme used, then I got dark system tray: image

Example used:

import time

from pystray import Menu, MenuItem, Icon
from PIL import Image
from tkinter import Tk
from tkinter.filedialog import askopenfilenames

def ref(text: str):
    return f'&{text[0]}\u0332{text[1:]}'

menu = Menu(
    MenuItem("Disabled", None, enabled=False),
    Menu.SEPARATOR,
    MenuItem(
        ref("checkbox"),
        None,
        checked=lambda _: True,
    ),
    MenuItem(
        ref("Disabled checkbox"),
        None,
        checked=lambda _: True,
        enabled=False
    ),
    Menu.SEPARATOR,
    MenuItem(
        ref('list'),
        Menu(
            MenuItem(ref('First'), None, radio=True, checked=lambda *_: True, default=True),
            MenuItem(ref('Second'), None, radio=True, checked=lambda *_: False),
            MenuItem(ref('Third'), None, radio=True, enabled=False),
        )
    ),
    Menu.SEPARATOR,
    MenuItem(ref('Exit'), None),
)

tray = Icon(
    "tray test",
    icon=Image.new('RGB', (64,)*2, 'green'),
    menu=menu,
)
tray.run_detached()
Tk().withdraw()
_ = askopenfilenames(title='Some explorer dialog')
time.sleep(20)
tray.stop()
time.sleep(1)
moses-palmer commented 1 year ago

Thank you for your report.

Since you get a dark menu when using Tkinter, I presume that that framework calls the SetPreferredAppMode win32 function. It appears to be undocumented and exported only by ordinal.

By adding the following code before creating the system tray icon, I managed to get a dark menu: ctypes.windll['uxtheme.dll'][135](1), Please se here for information about what this actually does.