arvindh
arvindh

Reputation: 769

python - unable to add image to the GUI(tkinter) on windows

I am using python( my version is 2.7 ). I want to add an image to GUI (Tkinter) and then convert into executable format using pyinstaller. I did followed as on SO, and also as said on ActiveState

When i mention the image's path on the code, it works only if i run it directly. If i convert it to exe it doesnt open.

Changing the code as mentioned from other solutions, like by converting it into encoded string, it runs fine on linux. But on windows it throws error

code:

from Tkinter import *
from PIL import ImageTk, Image

logo = '''
----- encoded string -----
'''

root = Tk()
logoimage = Tkinter.PhotoImage(master=root, data=logo)
Label(root, image=logoimage).pack()
root.mainloop()

Change 1: The above code works on linux. On windows i get error on the line logoimage = Tkinter.PhotoImage(master=root, data=logo) as

NameError: name 'Tkinter' is not defined

Change 2: So i tries changing the line as logoimage = ImageTk.PhotoImage(master=root, data=logo). The error i get is

File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 88, in __init__
    image = Image.open(BytesIO(kw["data"]))
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 2330, in open
    % (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x00000000024BB150>
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x00000000024D49E8>> ignored

Change 3: But, if i change the line as iconImage= ImageTk.PhotoImage(Image.open('path_to_image.png')). It works only if i run directly. If i convert it to executable, then console opens for 2-3 seconds and displaying error something like Unable to locate the image file

Upvotes: 1

Views: 2461

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55499

Doing the decoding and converting explicitly may be more robust than what you're currently doing. This code works on Python 2.6.6 on Linux.

import io, base64
from Tkinter import *
from PIL import ImageTk, Image

#A simple 64x64 PNG fading from orange in the top left corner 
# to red in the bottom right, encoded in base64
logo_b64 = '''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIA
AAAlC+aJAAAA/0lEQVR4nO3Zyw7CMAxEUdP//+W2rCqBoJA2noclS1kn9yjLeex7xKY76+
wNS+l6KSCjXgdIqhcB8uoVgNR6OiC7ngsA1BMBmHoWAFZPASDr8QBwPRiAr0cCKPUwAKse
AyDWAwDc+mwAvT4VoKjPA4jqkwC6+gyAtD7WSYC6fu4HDOonAB71dwE29bcATvXXAWb1Fw
F+9VcAlvXDANf6MYBx/QDAu/4fwL7+J6BC/TmgSP0JoE79N0Cp+g9Atfp3QMH6F0DN+gNQ
tj62WErXB2PgQNZLAb3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3Uz7
z+BNmX4gqbppsaAAAAAElFTkSuQmCC
'''

#Decode the PNG data & "wrap" it into a file-like object
fh = io.BytesIO(base64.b64decode(logo_b64))

#Create a PIL image from the PNG data
img = Image.open(fh, mode='r')

#We must open the window before calling ImageTk.PhotoImage
root = Tk()

photo = ImageTk.PhotoImage(image=img)
Label(root, image=photo).pack()
Label(root, text='An embedded\nbase64-encoded PNG').pack()
root.mainloop()

For reference, here's what that embedded PNG looks like.

fading from orange in the top left corner to red in the bottom right

Upvotes: 2

Kevin
Kevin

Reputation: 76254

from Tkinter import *
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)

If you dump the Tkinter module straight into the global scope using import *, then you shouldn't prefix class and function names with the module name. Either remove the prefix, or remove the import *.

import Tkinter
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)

Or

from Tkinter import *
#...
logoimage = PhotoImage(master=root, data=logo)

I suspect you're not getting the error in Linux because your version of Python imports common modules automatically. Effectively, there's an invisible import Tkinter at the top of all your scripts.

Upvotes: 0

Related Questions