i am trying to include a iconbitmap in my Tkinter project, but i want it to be a single .exe file. For that reason i dont want to use other files, that are needed (like in the same directory). My idea was, to just use an URL of a picture. What i found was that:
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(file="icon.png"))
How is it possible to use a URL instead? Or maybe there is a completely other way to store the icon, except as a seperate file.
CodePudding user response:
You can embed PhotoImage data as base64 and include it directly in the code. Take the contents of a file of any supported file type (eg: gif, png for newer versions of python) and encode it with base64.
The following code defines some image data, then uses it to create a PhotoImage
. The resulting image can be used anywhere a PhotoImage
can be used.
image_data = """
R0lGODlhGAAYAPZvAAIaLgwmOwcnQQcoQBMxSBc4VBQ6Vhk7VRY9WhY XSI4RxJC
ZRRCZi9KYSFNbzJUbSJOcCZVeiZUeyBWfy1Zei5afB1Wgh1XgyFXgDpjgSVjkyZl
liZnmCxrmixsmjZtlSptoCpuoSxxqCxzqjh1oTV3qDR4qD5/rjV8tDV tUFlgUZp
hFNwhVVyh0ZzlUh7nnB8hUF8pkJ8pkx oTuBuFmBn3SEkHeOn3mNnUeBqUiBqUqD
q1CBpFKCpVuDoVyEokeGs0eHs0aKvkaMvlGOuGqKom Pp3yUpziEwTiFwz2KxT K
xE6VylKUw1qaxV6fx0CQ0E b1lCd1l6h0Wiq1Wyu12Wp22aq3FOi4F2s5WCt5W64
7XW24Xu743u/6omfsZymroqhso6jtJynsJ2psaS1w6S2xIvH7YvI7d/h5ODi5eDk
5 Dm6uLn7OPo7QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAACH SUNvcHlyaWdodCBJTkNPUlMgR21iSCAod3d3Lmljb25l
eHBlcmllbmNlLmNvbSkgLSBVbmxpY2Vuc2VkIHByZXZpZXcgaW1hZ2UAIfkEBQAA
bwAsAAAAABgAGAAAB9eAb4KDhIWGh4iJiouMjY6PiGkwCgAACjBqj2ABBSw3Ny0H
AWONYwIqYmFfX2FiKwNki2kEFEZFRba3RhUEa4o2Cy41NT/FPsMuDDaKDRYfLzM8
PT0zMy8fFw2KBRoeJDI6Ojs5MSQdGwaKCCAmJ0FE8ERAJyUhCYoPIjRDTU5PT06a
CKEx4oEiHBuWMJlCpUoVKlOYKOFwQxEbB0ikXOHSpQsXK1GSQGizyIwFKFq8oDnj
JQsUDGYalYmAYsqWLVhSSIjpqM2RDBMuZDjiBpLRo0iTKjUaCAA7
"""
the_image = tk.PhotoImage(data=image_data)