I have problem with converting my python script to exe file. I use pyinstaller.
This fatal error occurs, when i try to launch exe file.
I tried different configuration of flags:
--windowed --noconsole --onefile --noupx
but it didn't solve problem.
This is only part of my script (GUI):
#GUI
window = tkinter.Tk()
window.title("SAPC - Scanned Aerial Photographs Correction")
window.geometry("300x300+500+200")
window.iconbitmap(default='favicon.ico')
logo1=PIL.Image.open("logo.png")
logo2 = PIL.ImageTk.PhotoImage(logo1)
tkinter.Label(window, text="Ver.1.0 Beta")
tkinter.Label(window,image=logo2).grid(row=0, column=0, columnspan=2, rowspan=2, sticky=tkinter.N+tkinter.E+tkinter.S+tkinter.W)
menubar=tkinter.Menu(window)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=openfile)
filemenu.add_command(label="Exit", command=window.quit)
editmenu = tkinter.Menu(menubar, tearoff=0)
editmenu.add_command(label="Template selection", command=lambda: mainfunction())
editmenu.add_command(label="Marker measurement", command=lambda: pomiarznaczka())
editmenu.add_command(label="Matching", command=lambda: testbutton())
editmenu.add_command(label="Accuracy analysis", command=lambda: analizadok())
editmenu.add_command(label="Transform", command=lambda: transformacja())
editmenu.add_command(label="Mask generator", command=lambda: maskgenerator())
menubar.add_cascade(label="File", menu=filemenu)
menubar.add_cascade(label="Workflow", menu=editmenu)
window.config(menu=menubar)
window.mainloop()
And now when i deleted part of code with loading icon and logo from files and again convert py script to exe file... it works fine.
How i solve this problem? Icon and logo are necessary.
I'm guessing that the script can't find the image files and fails because there's no code to handle this exception.
I would include the images in the script itself or in a separate python file that you import in the main script. That way you can control the paths and it also eliminates the need for bundling images along with your exe file. Check out my answer to this question for a full demo of this approach.
The gist of it...
1 - Convert the images to base64 strings
import base64
with open(img_input, "rb") as f:
with open(img_output_b64, "wb") as f2:
f2.write(base64.b64encode(f.read()))
2 - Paste the contents of the newly written file, img_output_b64
, into the main script you've posted here; maybe in a dictionary that also holds the file name and a hash of the original image (so you can ensure integrity later on). Decode and write the image to a file again.
import base64
with open(original_image_filename, "wb") as f:
f.write(base64.b64decode(image_encoded_as_base64))
3 - Now, you should be able to load it like you're doing in the script you've posted here.
window.iconbitmap(default=original_image_filename)