moses-palmer/pystray

Icon and Menu not shown when running from virtualenv (Poetry)

okelet opened this issue · 4 comments

Environment:

  • Ubuntu 22.04
  • Python 3.10
  • Poetry 1.1.14
  • Pystray from pip (poetry init, poetry add pystray)

Running the code below (taken from the examples) without Poetry runs fine (python3 test.py), but with Poetry (poetry run python3 test.py) the icon is not shown (just an empty space), nor is the menu when clicked; the tooltip (test name) is shown in both cases.

from pystray import Icon, Menu, MenuItem
from PIL import Image, ImageDraw

def create_image(width, height, color1, color2):
    # Generate an image and draw a pattern
    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

# In order for the icon to be displayed, you must provide an icon
icon = Icon(
    'test name',
    icon=create_image(64, 64, 'black', 'white'),
    menu=Menu(
        MenuItem('Click', lambda: print("Clicked")),
    )
)

# To finally show you icon, call run
icon.run()

Found this post with a similar problem, that fixed it using vext. Tried and works fine for me.

Perhaps, a reference to this in the README would be useful for other people.

From reading the answers to the linked question, it seems that installing pygobject would be the preferable way to solve the problem. Did you try that? That is what the documentation currently recommends.

vext appears to just provide access to the system packages, so installing python-gi or similar on the end user system is still required.

I will close this issue due to lack of feedback. If you think this is incorrect, please reopen.

Sorry, this was a side personal project, that is currently a bit abandoned. Will try to update in a few days.