'pyinstaller single EXE file - ico image in title of tkinter main window
I found some information here on Stack Overflow about this and have been trying it out, but it does not work for me. I need assistance from the community :)
As the title suggests, I want to add an *.ico to my tkinter windows. The problem is when creating a single EXE file using pyinstaller, this is not very straight forward. So below is exactly what I implemented from information gathered on Stack Overflow. Using Python 3.5 on 64 bit machine.
The name of my script is calculator.py The name of the ico is calculator.ico
1. I added the following lines of code to my Python script:
import os
import sys
datafile = "calculator.ico"
if not hasattr(sys, "frozen"):
datafile = os.path.join(os.path.dirname(__file__), datafile)
else:
datafile = os.path.join(sys.prefix, datafile)
window = tkinter.Tk()
window.iconbitmap(default=datafile)
2a. I then built my single EXE file with pyinstaller using the following commands:
pyinstaller -w -F -i "C:\PythonProjects\Calc\calculator.ico" calculator.py
2b. I also tried building my single EXE file with the following commands to see if it would help:
pyinstaller --onefile --windowed --icon=calculator.ico calculator.py
3. After pyinstaller built the single EXE file, I then added some content to the *.spec file - below is the complete *.spec file with added content:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['calculator.py'],
pathex=['C:\\PythonProjects\\calc'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries + [('caclulator.ico', 'C:\\PythonProjects\\calc\\calculator.ico', 'DATA')],
a.zipfiles,
a.datas,
name='calculator',
debug=False,
strip=False,
upx=True,
console=False , icon='calculator.ico')
I added the following to exe = EXE:
+ [('caclulator.ico', 'C:\\PythonProjects\\calc\\calculator.ico', 'DATA')]
After adding the above content to the spec file, I ran the following command.
pyinstaller calculator.spec
After the EXE is rebuilt, I run the EXE file and I receive the following error message:
- Failed to execute script calculator
Thanks in advance!
Solution 1:[1]
You did everything right. But you just missed one last step. Add the following lines in your code and it should work.
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
And then call this.
window.iconbitmap(default=resource_path(datafile))
Solution 2:[2]
all you need to do is to place the .ico image inside the root folder of your .py file and then when creating a .exe from your .py file using pyinstaller you type the following "pyinstaller --onefile --icon=my.ico my.py", specifying the name of the icon it must be a .ico file if not it won't work.
Solution 3:[3]
To Remove the default feather icon in your window move the icon to the root folder of your .py file and use the iconbitmap method and specify the name of the icon, for example... root = tk.Tk() root.iconbitmap("myIcon.ico")
Solution 4:[4]
Experienced same problem, solve in the following way:
--icon parameter doesn't add the ico-file to the exe-package, you need to add it additionally by --add-data parameter:
pyInstaller ... --add-data "icon.ico;." script.py
Now it will appear in temporary _MEIPASSxxxx folder when running the exe file and you can feed it to .iconbitmap method as suggested by Satyendra Sahani
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 | Satyendra Sahani |
| Solution 2 | Oden Ikpi David |
| Solution 3 | Oden Ikpi David |
| Solution 4 | avlmrsn |
