0
votes

I'm writing a widget with tkinter to emulate some search bars ( like google / champion select etc . ) within the widget there are some images. These images are in the same folder where I have my file .py and their path is C:\Users\HANZO\Desktop\tools\google.gif etc . till here the program works as intended

Now I tried to get the .exe through py2exe with this setup.py:

from distutils.core import setup
import py2exe

Mydata_files = [('images', ['C:\Users\HANZO\Desktop\tool\google.gif'], ['C:\Users\HANZO\Desktop\tool\champ.gif'])]

setup(
windows = [
    {

        "script": "multisearch.py",
        "icon_resources": [(1, "search.ico")]
    }
],
data_file=Mydata_files,
)

when I run the exe it runs as intended , but if I take the whole dist folder and move to another computer the .exe file gives me error saying it can not find the path c:\user\HANZO\desktop\tool\google.gif

and it is obvious that gives me error if it still use the old path. I assumed that my setup.py to include images in the lib folder, or something similar. I'm a noob both py2exe that tkinter.

how should I handle these images ?

1
One route that you could take, since you've stated they're in the same directory, and I've used in my tkinter programs that I've compiled is that instead of specifying a specific path on your computer, which won't work when copied to another computer is to simply specify the image such as image = "google.gif" which will grab the image out of the current directory, and then simply copy the images into the dist folder. - user4746908
@Skeletor did you mean in the setup.py or in the image label into the .py scrip? - HANZO

1 Answers

1
votes

Going to write an answer detailing some routes you could take as the comment section is a bit hard to do this in imo.

One route you could take is to not specify a full path such as you have done in the setup script, as this won't work when transferred to another computer as the path will not be the same, since you've hard-coded your computer's specific path instead of specifying the arbitrary path. I'll include how to do that later. So you could change your script to this as you have stated that the images are in the same directory as the script.

from distutils.core import setup
import py2exe

setup(
    windows = [
    { "script": "multisearch.py", "icon_resources": [(1, "search.ico")]}] )

As this will just compile your script into a run able .exe. Now, in your script if you could change, I'm assuming your still specifying full paths, the full path of the image to simply say google.gif as this will look for the image in the current working directory of the script. Now, after compiling you could simply copy the images over to the same directory as the compiled application again and it will work the same.

Alternatively, if you want to specify full path's you could do something like the following. If you don't mind using the built in os and sys modules.

PATH = os.listdir(os.path.dirname(os.path.abspath(sys.argv[0])))

you can read a similar question I posted yesterday about why it's sys.argv[0] instead of __file__. So, if the images are still in the same directory you could use this for a full path, but it's a bit messy imo. Alternatively, if you have the images in sub-folders etc you could walk over the directories.

Then in your setup.py script you could do something like the following to grab all say .gif images and include them in the compiling process.

image_files = []
for files in PATH:
    if files.endswith('.gif'):
        image_files.append(files)

Now you could do this in your script to include say all of the .gif images found above.

setup(
      windows = [
     { "script": "multisearch.py","icon_resources": [(1, "search.ico")]}],
     data_file=image_files,
     )