How to add an image in Tkinter? How to add an image in Tkinter? tkinter tkinter

How to add an image in Tkinter?


Python 3.3.1 [MSC v.1600 32 bit (Intel)] on win32 14.May.2013

This worked for me, by following the code above

from tkinter import *from PIL import ImageTk, Imageimport osroot = Tk()img = ImageTk.PhotoImage(Image.open("True1.gif"))panel = Label(root, image = img)panel.pack(side = "bottom", fill = "both", expand = "yes")root.mainloop()


There is no "Syntax Error" in the code above - it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.

The example above worked fine for me, testing on the interactive interpreter.


Here is an example for Python 3 that you can edit for Python 2 ;)

from tkinter import *from PIL import ImageTk, Imagefrom tkinter import filedialogimport osroot = Tk()root.geometry("550x300+300+150")root.resizable(width=True, height=True)def openfn():    filename = filedialog.askopenfilename(title='open')    return filenamedef open_img():    x = openfn()    img = Image.open(x)    img = img.resize((250, 250), Image.ANTIALIAS)    img = ImageTk.PhotoImage(img)    panel = Label(root, image=img)    panel.image = img    panel.pack()btn = Button(root, text='open image', command=open_img).pack()root.mainloop()

enter image description here