'Embed icon in python script

Does anybody know a way to embed an icon in a Python script such that when I create my standalone executable (using pyinstaller) I don't need to include the .ico file? I know this is possible with py2exe, but in my case I have to use Pyinstaller, as I was not successful using the former. I am using Tkinter.

I know about iconbitmap(iconName.ico) but that doesn't work if I wanna make a onefile executable.



Solution 1:[1]

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 base64
import os
from Tkinter import *
##The Base64 icon version as a string
icon = \
""" REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON
"""
icondata= base64.b64decode(icon)
## The temp file is icon.ico
tempFile= "icon.ico"
iconfile= open(tempFile,"wb")
## Extract the icon
iconfile.write(icondata)
iconfile.close()
root = Tk()
root.wm_iconbitmap(tempFile)
## Delete the tempfile
os.remove(tempFile)

Hope it helps!

Solution 2:[2]

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 tk

icon = """
    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 3:[3]

Solution by ALI3N

Follow these steps:

  1. 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=....
       )
  1. 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)
  1. 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

Solution 4:[4]

This worked for me:

from tkinter import  PhotoImage
import base64
img = """
REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON
"""
img= base64.b64decode(img)

root = Tk()
img=PhotoImage(data=img) 
root.wm_iconphoto(True, img)

Solution 5:[5]

import Tkinter as tk

icon = """
REPLACE THIS WITH YOUR BASE64 VERSION OF THE ICON
"""

root = tk.Tk() 
root.iconphoto(True, PhotoImage(data=icon))

Convert a .png file instead of an icon, also using utf-8 encoding with the same code above worked great for me!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saúl Pilatowsky-Cameo
Solution 2 freakrho
Solution 3 Community
Solution 4 D V
Solution 5 Jessie Wilson