Menu

images_location

2020-06-14
2020-06-29
  • Eric Mathieu

    Eric Mathieu - 2020-06-14

    Me again, this time for a suggestion.
    I am fianlizing my application by making it a one-file exe with pyinstaller. Since my app include an image I read a bit about how to include it in the exe and then how to access it at runtime (https://pyinstaller.readthedocs.io/en/stable/runtime-information.html) . To make a long story short, the current coding in the Python GUI is not working: prog_location set by argv[0] is '' when set from the exe and so the image cannot be found. I patched the GUI from the explanation found in the provided above link and everything is working fine.

    replaced
    photo_location = os.path.join(prog_location,"SECRET.png")
    with
    images_location = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(file)))
    photo_location = os.path.join(images_location,"SECRET.png")

    My suggestion is then: would it be possible to have a
    ...
    def images_location(prog_location):
    return prog_location
    ...
    included in the support file and call in the GUI file to get the images location.
    Everyone would then be able to modify it to suit its needs.

     
  • Greg Walters

    Greg Walters - 2020-06-14

    First, please let me say that I know VERY little about pyinstaller other that I've never gotten it to work for me. I run Linux, so it's somewhat a moot point.

    Now to your suggestion. Page allows you to specify an image file for many widgets. That makes it simple for quick Rapid Application Development (RAD) situations. However, when you utilize this feature, you do loose some functionallity when using images, since those definitions are placed in the GUI.py file.

    I prefer leaving the widgets somewhat pristene in the GUI file and loading the images from a function in the support.py file. This gives me ultimate control and flexability. If you are going to use a button or label as the holder for an icon ( .png format), then make the widget about 4 to 6 pixels larger than the icon. Since most icons are 32 bits square, I use a size of 40x40.

    Yes, you can create a simple function ( or even create a simple global variable ) that handles the path to your icon and graphic files.

    Many times, I use the following method to take care of the issue...

    global path1
    path1 = os.getcwd()
    fix_path()
    ...
    imgpath = path1 + "/images/"
    img = Image.open(path1 + '/images/document.png')
    shared.folder = ImageTk.PhotoImage(img)
    

    and the "magic" function fix_path() (which normally isn't needed) is...

    def fix_path():
        global path1
        if "main" in path1:
            pass
        else:
            path1 = path1 + "/main"
    

    This takes care of starting the program outside of the main folder. It also assumes that all the images are located in the /images/ folder. Of course under Windows, you would use "\images\" I believe.

    Hope that again this helps.

    Greg

     
  • Anonymous

    Anonymous - 2020-06-29

    For pyinstaller, you need to specify the data to include in the final package. This can be done in the .spec file that gets generated after the first time you run pyinstaller (and will be used as the template rather than autogenerated after that if you save it, depending on command-line options), or you can look into writing a hook file which can be added at the command line. I've done it both ways, but ended up using the hook file (it's uses something like --additional-hook-directory

    command line option, check the docs website for how to create them and --help for the correct flag) - mainly because then I just needed to update the command line and didn't need to mess with a manual .spec file.

    Basically, you can tell pyinstaller to add specific files, entire folders, etc. as separate data files into the final package so they can be found by your script just like it was in the filesystem when run as scripts. I had to do this for metadata files where my version was being stored.

     
  • Greg Walters

    Greg Walters - 2020-06-29

    Thanks for the information.
    I'm going to be upgrading my machine soon, so once that happens, I'll check it out.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.