0

I have written a small program in tkinter in python 3.5

I'm making executable out of it using pyintaller

I have included a custom icon to the window to replace default feather icon of tkinter

from tkinter import *
from tkinter import messagebox
import webbrowser

calculator = Tk()
calculator.title("TBE Calculator")
calculator.resizable(0, 0)
iconFile = 'calculator.ico'

calculator.iconbitmap(default=iconFile)

icon works fine when running program.py file directly

But when making it executable using

pyinstaller --onefile --windowed --icon=program.ico program.py

and running program.exe from dist directory, it gives error as

failed to execute script program

I also tried with

pyinstaller --onefile --windowed --icon=program.ico --add-data="calculator.ico;ico" program.py

But still same error.

program.spec file

# -*- mode: python -*-

block_cipher = None


a = Analysis(['program.py'],
             pathex=['C:\\Users\\anuj\\PycharmProjects\\YouTubePlayer\\Program'],
             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,
          a.zipfiles,
          a.datas,
          name='calculator',
          debug=False,
          strip=False,
          upx=True,
          console=False , icon='program.ico')

Removing the line calculator.iconbitmap(default=iconFile) works fine but with default feather icon.

How to include window icon file with .exe executable?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

3 Answers3

1

Try to provide absolute path for your icon file as well as bundling .ico file via datas PyInstaller argument.

datas should be list of tuples like ('what_to_bundle.ext', 'there_to_place_it') (see docs), so add something like

...
binaries=[],
datas=[('calculator.ico', '.')],
hiddenimports=[],
...

Thus, calculator.ico will be placed in root dir of your app (then bundling to one file, pyinstaller creates an executable which extracts all files in temp folder and then run as usual in one-dir mode).

Next, you can find a path to icon file from your script using sys._MEIPASS variable set by PyInstaller (see this excellent answer). This should do the trick:

import os
import sys
from tkinter import *
from tkinter import messagebox
import webbrowser

calculator = Tk()
calculator.title("TBE Calculator")
calculator.resizable(0, 0)

if getattr(sys, 'frozen', False):
    application_path = sys._MEIPASS
elif __file__:
    application_path = os.path.dirname(__file__)

iconFile = 'calculator.ico'

calculator.iconbitmap(default=os.path.join(application_path, iconFile))
calculator.mainloop()
9dogs
  • 1,446
  • 10
  • 18
1

I know it is late but try using

pyinstaller --onefile --windowed --icon program.ico program.py
a1234910
  • 47
  • 7
0

Just to add here, for me the problem was that I was writing the path with '/' to separate diretories and windows wants you to use '\\'.

exemple: path/to/icon.ico -> falied path\\to\\icon.ico -> succeeded

This in the .spec as you putted in your file.