Home > OS >  Why there is an error occurred by pystary in my programme (AttributeError: 'Icon' object h
Why there is an error occurred by pystary in my programme (AttributeError: 'Icon' object h

Time:08-15

An error occurred by pystary

My coding enviroment:windows10, python3.7

I want to make a tray application.But an error occurred.

Here is my code:

from pystray import MenuItem as item
import pystray
from PIL import Image


def show():
    print(":D")

image = Image.open("TrayIcon.jpg")
menu = (item('print(":D")', show))
icon = pystray.Icon("name", image, "title", menu)
icon.run()

Here is the error:

Traceback (most recent call last):
  File "C:/Users/admin/AppData/Roaming/JetBrains/PyCharmCE2022.1/scratches/scratch.py", line 12, in <module>
    icon = pystray.Icon("name", image, "title", menu)
  File "D:\py3.7\lib\site-packages\pystray\_win32.py", line 32, in __init__
    super(Icon, self).__init__(*args, **kwargs)
  File "D:\py3.7\lib\site-packages\pystray\_base.py", line 89, in __init__
    else Menu(*menu) if menu is not None \
TypeError: type object argument after * must be an iterable, not MenuItem
Exception ignored in: <function Icon.__del__ at 0x000001CC7BE15EA0>
Traceback (most recent call last):
  File "D:\py3.7\lib\site-packages\pystray\_win32.py", line 50, in __del__
    if self._running:
AttributeError: 'Icon' object has no attribute '_running'

CodePudding user response:

menu must be an iterable (a list or a tuple) but currently it is just a single item. You need to add a comma to make it a tuple:

menu = (item('print(":D")', show),)

In python, (42) is just the number 42, but (42,) is a tuple -- an iterable object -- containing the number 42.

  • Related