0

I am creating an .exe from my gui.py i have several pictures in it which are implemented with:

from Tkinter import*
from PIL import ImageTk

bild = ImageTk.PhotoImage(file=r"C:\something\somehow\my.png")
label5 = Label(toplevel1, image=bild)
label5.image = bild
label5.pack(side=TOP)

(this is just a small piece of my code. It won't run by itself it should just show how i implemented my picture. If you need more code please tell me)

now i created the exe and i can't start it from another pc on which the my.png isn't in the C:\something\somehow directory. Can i bind pictures permanently to a code or can i implement a function that looks for that specific picture on the whole pc.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
python_beginner
  • 123
  • 1
  • 8
  • i am using py2exe btw – python_beginner Jul 25 '15 at 15:21
  • 2
    How about bundling the images (and other resources) together with the exe and using a relative path? – tobias_k Jul 25 '15 at 15:23
  • yea i think that would work. i tried to copy it in the same directory as on my first pc, that worked well. Do you know how i can make the path relative? sry to ask that but i never used that before. – python_beginner Jul 25 '15 at 15:29
  • You can make the path relative by using `.` and `..`. The first is the current directory and the other is up-on-level from the current directory , Alternatively you may be able to use the trick shown in [this answer](http://stackoverflow.com/a/30509487/355230) if your modules have `__file__` attribute after being turned into an .exe. – martineau Jul 25 '15 at 15:37
  • do the dots work if i dont know in which "layer" the file is ? if yes can you give me an example? Sry i don't find much in google – python_beginner Jul 25 '15 at 15:46
  • your linked answer doesn't work for me as well. – python_beginner Jul 25 '15 at 15:50
  • somehow the __file__ statement doesn't work – python_beginner Jul 25 '15 at 15:51
  • It doesn't surprise me that it doesn't work after conversion to an .exe. A viable alternative would be to convert the image data into a Python literal and include it in a .py file. See [_convert image to byte literal in python_](http://stackoverflow.com/questions/29507890/convert-image-to-byte-literal-in-python) for more information. – martineau Jul 25 '15 at 15:56
  • P.S. To answer your earlier question, the dots are only useful if you know what directory to make them relative to, such as the current working directory (or the directory portion of the `__file__` path variable if it's defined as it is in under normal circumstances). – martineau Jul 25 '15 at 16:07
  • i am runnig out of options. can i set the directory to the folder in which my exe is in. for example if i unzip the data on my desktop its C:\desktop\myfolder. in that directory all the .png files are in an extra folder and the exe is in it as well. – python_beginner Jul 25 '15 at 16:19
  • 1
    Instead of assuming where the .exe file is and hardcoding a path, I suggest you use `sys.argv[0]` which py2exe sets to the full pathname of the executable (according the [this documentation](http://www.py2exe.org/index.cgi/Py2exeEnvironment)). That means the folder the .exe is in could be obtained with `exe_dir = os.path.dirname(sys.argv[0])`. – martineau Jul 25 '15 at 17:11
  • The answers to the question [_py2exe - generate single executable file_](http://stackoverflow.com/questions/112698/py2exe-generate-single-executable-file) — especially the one about using `bundle_files` — sound like a better way to do something like this. – martineau Jul 25 '15 at 17:35
  • Hello martineau! Ty very much for your help. I got it now! It worked with setting the current working directory with os.chdir(os.path.dirname(sys.argv[0])) – python_beginner Jul 26 '15 at 06:39

0 Answers0