3

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

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
TEL51
  • 51
  • 5
  • Does it give further information when telling you that it failed to run myappsource? Perhaps a line that looks like "Could not find theIcon.ico"? – Scott Mermelstein Jul 09 '18 at 19:14
  • Not when I run the application. I also looked at the info generated by pyinstaller on the command line and see nothing to indicate it could not find the icon. The icon is in the same directory as the python source and was used properly as the application icon. – TEL51 Jul 09 '18 at 19:57
  • There's a difference between pyinstaller using the icon to set the icon for the executable (that's all "external" to your application), and pyinstaller including the files your application needs. Check out https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file , it will likely point you in the right direction. – Scott Mermelstein Jul 09 '18 at 21:10
  • 1
    I've been looking at the link you provided and links leading from that link. I think you are right, that the solution is somewhere in that direction. However, I'm fairly new to Python, tkinter, etc., so it's going to take me awhile to figure out what all of it means. And I really don't like putting code in my app that I don't fully understand. Thanks for pointing me on the right path. I've now got some experimenting to do using a very simplistic app that simply opens a window, replacing the feather icon with my own and all within a single exe file. – TEL51 Jul 10 '18 at 22:38

1 Answers1

0

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" 
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Thanks for the suggestion, but it wouldn't even start the compilation. The following error message was displayed: pyinstaller: error: argument --add-binary: invalid add_data_or_binary value: 'theIcon.ico' – TEL51 Jul 09 '18 at 19:58