Next / Previous / Contents / TCC Help System / NM Tech homepage

7. The ImageTk module

This module is for use with the graphical user interface functions of the Tkinter package. For more information about the Tkinter widget set, see the local Tkinter page.

To import the ImageTk module:

    import ImageTk

This module contains two class constructors:


Given an image i, constructs a BitMapImage object that can be used wherever Tkinter expects an image object. The image must have mode "1". Any keyword options are passed on to Tkinter. For example, the option foreground=c can be used to display the pixels of value 1 in color c, while pixels of value 0 are transparent.


Given an image i, constructs a PhotoImage object that can be used wherever Tkinter expects an image object.


There is a bug in the current version of the Python Imaging Library that can cause your images not to display properly. When you create an object of class PhotoImage, the reference count for that object does not get properly incremented, so unless you keep a reference to that object somewhere else, the PhotoImage object may be garbage-collected, leaving your graphic blank on the application.

For example, if you have a canvas or label widget that refers to such an image object, keep a list named .imageList in that object, and append all PhotoImage objects to it as they are created. If your widget may cycle through a large number of images, you will also want to delete the objects from this list when they are no longer in use.

ImageTk.PhotoImage(mode, size)

Creates an empty PhotoImage object with the given mode and size.

To change image data in a PhotoImage object, use this method:

.paste(i2, bbox=None)

Image data from an Image object i2 is pasted into the PhotoImage object. To paste the new data into a given location, specify the bbox argument as a bounding box; this argument can be omitted if i2 is the same size as the PhotoImage object.