else/QuickTips/tkinter images.md
Ethan Dalool 64de5c94bb else
2017-02-18 17:06:55 -08:00

747 B

Tkinter Images

When using Tkinter alone, you can only embed .gif images in your interface. PIL provides a PhotoImage class that lets you embed other supported file types.

Requires pip install pillow

import PIL.Image
import PIL.ImageTk
import tkinter

t = tkinter.Tk()
image = PIL.Image.open('filename.png')
image_tk = PIL.ImageTk.PhotoImage(image)
label = tkinter.Label(t, image=image_tk)
label.image_reference = image_tk
label.pack()

You must store the image_tk somewhere, such as an attribute of the label it belongs to. Otherwise, it gets prematurely garbage-collected.

Screenshot