How to draw images in tkinter window How to draw images in tkinter window tkinter tkinter

How to draw images in tkinter window


You will need to use a Canvas widget to put your images in specified (x,y) positions.

In Python 3, you can do like this:

import tkintertk = tkinter.Tk()can = tkinter.Canvas(tk)can.pack()img = tkinter.PhotoImg("<path/to/image_file>.gif")can.create_image((x_coordinate, y_coordinate), img)

Please note that due to Python 3 not having an official PIL* release, you are limited to read images of type GIF, PGM or PPM - if you need other file types, check this answer.

The Canvas widget is quite powerfull, and allows you to position your images, shows what is on it through an "canvas.update" call, and remove an item displayer with a "canvas.delete(item_id)" call. Check its documentation.

While Tkinter should be enough for your simple game, consider taking a look at Pygame, for a better multimedia support, or maybe Pyglet, or even higher level multimedia framework called Kivy.

*(update): As of 2015, there is Pillow - a fork that is a drop in replacement of the old PIL project, and which resumed proper development of the project, including support for Python 3.x


The example displays an image on the canvas.

from PIL import Image, ImageTk

From the PIL (Python Imaging Library) module, we import the Image and ImageTk modules.

self.img = Image.open("tatras.jpg") //your image name :)self.tatras = ImageTk.PhotoImage(self.img)

Tkinter does not support JPG images internally. As a workaround, we use the Image and ImageTk modules.

canvas = Canvas(self, width=self.img.size[0]+20,    height=self.img.size[1]+20)

We create the Canvas widget. It takes the size of the image into account. It is 20px wider and 20px higher than the actual image size.

canvas.create_image(10, 10, anchor=NW, image=self.tatras)

Reference see: https://tutorialspoint.com/python/tk_canvas.htm


This depends a lot on the file format. Tkinter has a PhotoImage class which can be used in Labels quite easily if your image is a .gif. You can also add them to canvas widgets reasonably easily. Otherwise, you might want to use PIL to convert an image to a PhotoImage.