0

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.

ReinerPing
  • 75
  • 1
  • 7
  • Here is a solution already resolved. https://stackoverflow.com/questions/38173526/displaying-images-from-url-in-tkinter – codecaine Feb 11 '22 at 17:18
  • It's possible to embed an image as base64-encoded data directly in your code. Would that be an acceptable answer or do you only want to be able to fetch it from a URL? – Bryan Oakley Feb 11 '22 at 17:36
  • @BryanOakley For me it doesnt really matter how. The only think i want is, that i can display the image as an iconphoto without any secondary file. So i think encoding it would be a good solution. Can you explain me how to do that? – ReinerPing Feb 11 '22 at 17:47

1 Answers1

1

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685