moses-palmer / pystray

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

No default action on Windows #27

Closed yanone closed 5 years ago

yanone commented 5 years ago

Hi.

Although one menu item of mine has the default flag set, nothing happens when I click on the icon. I construct it like so:

menu = (item(localize('Open Type.World App'), openApp, default=True), item(localize('Check for font updates now'), checkForUpdates))
icon = pystray.Icon("Type.World", image, "Type.World", menu)

Is more code needed? This is how I understood it works. (The default menu item does get rendered in bold in the menu, so something is happening for sure)

moses-palmer commented 5 years ago

That is all that should be needed. Can you please test the code below to verify that it works?

from PIL import Image, ImageDraw

from pystray import Icon
from pystray import Menu as menu
from pystray import MenuItem as item

clicks = []

def image(color1, color2, width=64, height=64):
    image = Image.new('RGB', (width, height), color1)
    dc = ImageDraw.Draw(image)

    dc.rectangle((width // 2, 0, width, height // 2), fill=color2)
    dc.rectangle((0, height // 2, width // 2, height), fill=color2)

    return image

def is_item_visible(item):
    return not len(clicks) % 2

def on_activate(icon):
    clicks.append(icon)

    if len(clicks) == 5:
        print('stopping...')
        icon.stop()
    else:
        print('updating icon...')
        icon.icon = images[len(clicks) % len(images)]

images = (image('white', 'black'), image('black', 'white'))
icon = Icon(
    'test',
    icon=images[0],
    menu=menu(
        item(
            'Item 1',
            lambda icon: print('Hello 1!'),
            visible=is_item_visible),
        menu.SEPARATOR,
        item(
            'Item 2',
            on_activate,
            default=True,
            visible=is_item_visible)))

print('OK, here we go...')
icon.run()
Romzes25 commented 5 years ago

Здравствуйте! Подскажите как использовать ваш проект с программой tkinter? заранее спасибо !

moses-palmer commented 5 years ago

@Romzes25, this should be addressed in a new issue, but since GitHub does not appear to have an easy way of transferring a comment to another issue, I will answer here.

Using pystray with Tkinter, or really any other GUI framework, is not supported on all platforms; OSX requires GUI events to be handled from the main thread, which makes it awkward to use multiple GUI frameworks.

If that is not an issue for you, you can simply create a new thread for your pystray icon, and run all initialisation there, and then launch Tkinter as you would normally do.

moses-palmer commented 5 years ago

I will close this issue due to lack of a response. If you think it still persists, please reopen.