0

I'm trying to display an image on a toplevel widget with tkinter.

When I run the code from the python file it all works well, but when I use pyinstaller the image disappears without giving any errors.

PyInstaller command:

pyinstaller --noconfirm --onefile --windowed --icon "C:/Users/Giovanni/Desktop/passgen/key.ico"  "C:/Users/Giovanni/Desktop/passgen/spg.py"

Here's the code that I've tried:

Attempt 1

enimg = tkinter.PhotoImage(file="en.png")
enpanel = tk.Label(tutoriallevel, image=enimg)
enpanel.pack()

Attempt 2

from PIL import ImageTk
enimg = ImageTk.PhotoImage(file="en.png")
enpanel = tk.Label(tutoriallevel, image=enimg)
enpanel.pack()

Attempt 3

from PIL import ImageTk, Image
enimg = ImageTk.PhotoImage(Image.open("en.png"))
enpanel = tk.Label(tutoriallevel, image=enimg)
enpanel.pack()

Does anyone knows a solution? Thanks in advance.

EDIT: I've also tried using the --adddata argument with PyInstaller but it still doesn't show the image. New PyInstaller command:

pyinstaller --noconfirm --onefile --windowed --icon "C:/Users/Giovanni/Desktop/passgen/key.ico" --add-data "C:/Users/Giovanni/Desktop/passgen/en.png;." --add-data "C:/Users/Giovanni/Desktop/passgen/ita.png;."  "C:/Users/Giovanni/Desktop/passgen/spg.py"
gioggino
  • 3
  • 7
  • The file you're using has a contains no path so it will be interpreted as relative to the current working directory, which means the .png image file will only be found if it's in that directory, whatever it is, when the .exe is run. In a normal Python script you can use `__file__` to determine where the script file is located. There is something similar in pyinstaller — look in it's documentation for "runtime information". – martineau Sep 02 '21 at 11:05
  • have a look at [this](https://stackoverflow.com/questions/60937345/how-to-set-up-relative-paths-to-make-a-portable-exe-build-in-pyinstaller-with-p/60945435#60945435) answer, discussing paths to external files with PyInstaller. – wstk Sep 08 '21 at 11:12

1 Answers1

1

If displaying an image on a .exe bundled by Pyinstaller is what you want, I will show you a alternative that worked for me.

1: You need to encode the image file. It needs to be a GIF format. you can do so by using the code bellow:

import base64
with open("YourPictureFile.gif", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
print(encoded_string)#print string to copy it (see step 2) 

2: Now you need to copy the string that was printed and create a new python file named "myimages.py" (or whatever name you want) and store it in a variable:

imgString = b'R84GjfmntoeiDLKjg55[...]'

3: after that, you need to go to your main program file and import "myimages.py" and call the variable:

from myimages import *
img = imgString # The variable of decoded image.
imgRendered = PhotoImage(data=img)
WindowLabel = Label(app, image=imgRedered)

After which, you can bundle you program with Pyinstaller and the image will show on your GUI.

If anyone comes up with a better working solution, I will be more than happy to read.