Is there a way to use a string for the iconbitmap
in the Tkinter
(Python 2.7.9) module?
I know that you can prodive a file path (even though I haven't understood where the difference between default
and bitmap
as parameters is.
The reason I am asking is because I want to create out of a Python script an .exe
with py2exe
(which works), but I would need to create a icon file then to be able to use an icon.
Any workaround or other method is appreciated.
(Note to folks using Python 3, see my supplemental answer for an alternative that only works in that version.)
I don't know of any way to pass
iconbitmap()
anything other than a file path in Python 2.x, so here's a workaround that creates a temporary file from a string representation of icon file's contents to pass it. It also shows a way to ensure the temporary file gets deleted.The window displayed will have a custom icon as shown below:
You didn't ask how to do it, but here's the code I used to convert the original
.ico
file into the Python string variable used in my example: