Search code examples
pythonmenupystray

Why does pystary menu raises so many errors?


I want to make tray icon with pystray. Howerer, it doesnt seem to work. I dont wonna make this question too long, so I gonna put error here. Code i'm using:

...
import sys
import pystray
from pystray import Icon as icon, Menu as menu, MenuItem as item

from PIL import Image, ImageDraw
state = False
def quit(icon,item):
     sys.exit(1)
m=(item(
    "Wyjdź",
    quit))
icon = pystray.Icon('AutoThemeChanger',title='AutoThemeChanger działa w tle',menu=m)
width = 120
height = 120
color1 = 255
color2 = 255

# Generate an image
image = Image.open("tray.png")
icon.icon = image
icon.visible=True


...

Can someone help me please?


Solution

  • Currently for the menu you are using a tuple and not the imported menu object. You should make the whole tuple a menu object like this with nested items and can also use submenus if you want:

    from pystray import Menu, MenuItem as Item
    
    menu = (
    Item('mainitem1', callable),
    Item('mainitem2', callable),
    Item('mainitem3', Menu(Item('subitem1', callable),Item('subitem2', callable))),
    Item('mainitem4', callable)
            )
    

    also to be noted, sys.exit() will not work in this scenario, you need to use icon.stop().