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

23 lines
No EOL
747 B
Markdown

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`
```Python
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](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm).
![Screenshot](/../master/.GitImages/quicktips_imagetk.png?raw=true)