Python - Application and Upper Left Corner Icons

1.3k Views Asked by At

Hopefully, there's a simple solution to my problem. I have a Python tkinter application and all is working as expected. I used the following command to generate a standalone exe file for distribution.

"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Scripts\pyinstaller" -w -F --i "theIcon.ico" "myappsource.py"

The executable worked just as expected. However, I realized that I had forgotten to put an icon in the upper left corner of my window. After a little online research, I determined that the following line had to be added to my code (note that I want the application icon and the upper left corner icon to be the same):

root.wm_iconbitmap('theIcon.ico')

With this being the only change, I ran the application in Visual Studio and it worked as expected, inserting the icon in the upper left corner. However, when I re-compiled the app using the same command as I used before, the application now will not run. I get a popup window titled "Fatal Error!" with "Failed to execute script myappsource" as the message. I've tried with and without the root.wm_iconbitmap line several times - one way it works, the other it doesn't. Any suggestions? Thanks

1

There are 1 best solutions below

1
Scott Mermelstein On

I've got a few guesses as to what's happening here.

  • By adding the root.wm_iconbitmap line, you're now creating a dependency on that file. tkinter will try to open theIcon.ico

  • However, your pyinstaller invocation only includes myappsource. So when tkinter looks for the icon, it fails, and therefore your program fails to run. (It works ok in Visual Studio because you have the file locally, but not in the installer.)

The solution is to add the file into your installer. Based on the pyinstaller docs, it looks like you want to use the --add-binary option.

Try this as your command line. (I truncated your long path to pyinstaller, just to make the significant arguments more visible.)

pyinstaller -w -F --i "theIcon.ico" --add-binary theIcon.ico "myappsource.py"