-1

I'm using auto-py-to-exe which is based on pyinstaller, trying to make an executable with --onefile, using pystray to make a tray app, but after compiling I get an error that the icon file cannot be found. See that in the code I tried to use os.path to get the most specific path possible, even so it is not possible to find.

The question is, how to make this path correct using --onfile?


import pystray
from PIL import Image
# ==============================
from os.path import join
from os import getcwd


TilleTables = SetInterval(1, TileTables)
# print(join(getcwd(), 'icon.png'))
menuImg = Image.open(join(getcwd(), 'icon.png'))

MenuIcon = pystray.Icon('Neural', menuImg, menu=pystray.Menu(
    pystray.MenuItem('Exit', HandleTrayClick),

))

MenuIcon.run()
Traceback (most recent call last):
  File "main.py", line 20, in <module>
  File "PIL\Image.py", line 3092, in open
FileNotFoundError: [Errno 2] No such file or directory: 'icon.png'

Auto-py-to-exe image

Error Screenshot

Collaxd
  • 171
  • 1
  • 11
  • uploaded code @RahulKP – Collaxd Oct 24 '22 at 22:22
  • yes that was my first try, the error is the same @RahulKP – Collaxd Oct 24 '22 at 22:26
  • How did you tell pyinstaller it needs that file? pyinstaller has no idea what files you need unless you tell it. – Tim Roberts Oct 24 '22 at 22:27
  • @TimRoberts Now I've added to a adicional files field, but same error https://i.imgur.com/F7uuqit.png – Collaxd Oct 24 '22 at 22:30
  • possibly a duplicate of this question [Pyinstaller executable saves files to temp folder](https://stackoverflow.com/questions/70405069/pyinstaller-executable-saves-files-to-temp-folder/70405825#70405825) and this question [How can you access files after being compiled to a single python executable?](https://stackoverflow.com/questions/73433075/how-can-you-access-files-after-being-compiled-to-a-single-python-executable/73433308#73433308) – Ahmed AEK Oct 24 '22 at 22:33
  • OK, but you need to know (a) where does pyinstaller put the file, and (b) what directory is the current directory when you run. – Tim Roberts Oct 24 '22 at 22:34
  • @TimRoberts ye, pyinstaller seem's me to placing on '.' https://i.imgur.com/ZA74eJH.png And now I'm trying to use this one example from auto-py-to-exe helps https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/13790741#13790741 But even if I use 'resource_path' function and set img `menuImg = Image.open(resource_path('icon.png'))` don't work – Collaxd Oct 24 '22 at 22:37
  • you can just open your temp folder and find the image location, it's usually in your `AppData\Local\Temp`, make sure you open it while your application is running ... like before you close the error message, or show us what your "current command" looks like – Ahmed AEK Oct 24 '22 at 22:39
  • @AhmedAEK solved, I added a temp and resources path! – Collaxd Oct 24 '22 at 22:52

1 Answers1

0

For future readers, the path of pyinstaller must be relative after the build, so it is necessary to add the function that creates a temporary file for the icon... Bundling data files with PyInstaller (--onefile)

the solution found was to add the patch to this function and within the auto-py-to-exe set the icon file to the additional files

menuImg = Image.open(resource_path('icon.png'))

MenuIcon = pystray.Icon('Neural', menuImg, menu=pystray.Menu(
    pystray.MenuItem('Exit', HandleTrayClick),

))

MenuIcon.run()

So the icon appears in the temp folder and can be found by the program.

Collaxd
  • 171
  • 1
  • 11