Search code examples
tkinterpyinstallerttk

Packing images together with the main file with pyinstaller --onefile problem


I'm building my first program with pyinstaller and I need to add the assets folder to the build, and in the script I use

tk.PhotoImage(file='./image.png')

This works petty good without --onefile

pyinstaller.exe --add-data 'assets;assets' --collect-data sv_ttk .\main.py

But when I use --onefile I have and FileNotFound error.. I think the problem is in using './' in my images, any solution?

No Such File or Dir


Solution

  • Solved with @acw1668 link with

    import sys
    from os import path
    
    def resource_path(relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        base_path = getattr(sys, '_MEIPASS', path.dirname(path.abspath(__file__)))
        return path.join(base_path, relative_path)
    

    and all my PhotoImages is now

    PhotoImage(file=resource_path('assets/icon.png'))
    

    Now I can build --onefile without problems

    *expect to help future users :)