Next / Previous / Contents / Shipman's homepage

5.9. Images

There are three general methods for displaying graphic images in your Tkinter application.

5.9.1. The BitmapImage class

To display a two-color image in the .xbm format, you will need this constructor:

    tk.BitmapImage(file=f[, background=b][, foreground=c])

where f is the name of the .xbm image file.

Normally, foreground (1) bits in the image will be displayed as black pixels, and background (0) bits in the image will be transparent. To change this behavior, use the optional background=b option to set the background to color b, and the optional foreground=c option to set the foreground to color c. For color specification, see Section 5.3, “Colors”.

This constructor returns a value that can be used anywhere Tkinter expects an image. For example, to display an image as a label, use a Label widget (see Section 12, “The Label widget”) and supply the BitmapImage object as the value of the image option:

    logo = tk.BitmapImage('logo.xbm', foreground='red')
    Label(image=logo).grid()

5.9.2. The PhotoImage class

To display a color image in .gif, .pgm, or .ppm format, you will need this constructor:

    tk.PhotoImage(file=f)

where f is the name of the image file. The constructor returns a value that can be used anywhere Tkinter expects an image.