Embed icon in python script
Actually the function iconbitmap can only receive a filename as argument, so there needs to be a file there. You can make a Base64 version of the icon (A string version) following the link, uploading the file and copying the result in your source file as a variable string. Extract it to a temporal file, finally passing that file to iconbitmap and deleting it. It's quite simple:
import base64import osfrom Tkinter import *##The Base64 icon version as a stringicon = \""" REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON"""icondata= base64.b64decode(icon)## The temp file is icon.icotempFile= "icon.ico"iconfile= open(tempFile,"wb")## Extract the iconiconfile.write(icondata)iconfile.close()root = Tk()root.wm_iconbitmap(tempFile)## Delete the tempfileos.remove(tempFile)
Hope it helps!
You probably don't need this but somebody else might find this useful, I found you can do it without creating a file:
import Tkinter as tkicon = """ REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON """root = tk.Tk()img = tk.PhotoImage(data=icon)root.tk.call('wm', 'iconphoto', root._w, img)
Solution by ALI3N
Follow these steps:
- Edit your .spec file like this:
a = Analysis(....)pyz = PYZ(a.pure)exe = EXE(pyz, a.scripts, a.binaries + [('your.ico', 'path_to_your.ico', 'DATA')], a.zipfiles, a.datas, name=.... )
- Add this to your script:
datafile = "your.ico" if not hasattr(sys, "frozen"): datafile = os.path.join(os.path.dirname(__file__), datafile) else: datafile = os.path.join(sys.prefix, datafile)
- Use it this way:
root = tk.Tk()root.iconbitmap(default=datafile)
Because this wont work after You compile your script with Pyinstaller:
root = tk.Tk()root.iconbitmap(default="path/to/your.ico")
My Info: python3.4, pyinstaller3.1.1