moses-palmer/pystray

Error in Windows

HTMofDreams opened this issue · 3 comments

In Debian 11, my code works very well but in Windows 10, I get this error:

Exception in thread Thread-2 (setup_handler):
Traceback (most recent call last):
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\site-packages\pystray_base.py", line 396, in setup_handler
self.visible = True
^^^^^^^^^^^^
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\site-packages\pystray_base.py", line 183, in visible
self._update_icon()
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\site-packages\pystray_win32.py", line 72, in _update_icon
self._assert_icon_handle()
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\site-packages\pystray_win32.py", line 349, in _assert_icon_handle
self._icon_handle = win32.LoadImage(
^^^^^^^^^^^^^^^^
File "C:\Users\u0\AppData\Local\Programs\Python\Python311\Lib\site-packages\pystray_util\win32.py", line 203, in _err
raise ctypes.WinError()
OSError: [WinError 0] Der Vorgang wurde erfolgreich beendet.


My code is this:

import pystray
from PIL import Image, ImageDraw
from iptool import IpInfo
import threading
import time

def after_click ():
	quit ()

def loop (ipinfo, icon):
	while True:
		ipinfo.get_info ()

		icon.icon = Image.open(ipinfo.flag_path)
		time.sleep (1)
				
if __name__ == '__main__':
	ipinfo = IpInfo ()
	ipinfo.get_info ()

	icon = pystray.Icon("flag", Image.open(ipinfo.flag_path), "",
		menu=pystray.Menu(
		pystray.MenuItem("Exit", after_click)))
	

	thread = threading.Thread(target=loop, args=(ipinfo, icon,))
	thread.start()
	icon.run ()

Thank you for your report.

It appears that a call to LoadImage fails. GetLastError is supposed to give more details, but none appear to be present.

The icon was previously generated by pystray by serialising the PIL image as an ICO file. You could try to set a breakpoint in pystray/_win32.py just inside the with-block in _assert_icon_handle and extract the actual file name to check whether the icon is correctly written. The file will be removed when you exist the with-block, so make sure to keep the application suspended while you check the image.

As to what could have gone wrong, I do not really have any good ideas. Perhaps the dimensions of the image are unsupported?