There are three general methods for displaying graphic images in your Tkinter application.
To display bitmap (two-color) images in the .xbm
format, refer to Section 5.9.1, “The BitmapImage
class”.
To display full-color images in the .gif
, .pgm
, or .ppm
format, see Section 5.9.2, “The PhotoImage
class”.
The Python Imaging Library (PIL) supports images in a
much wider variety of formats. Its ImageTk
class is specifically designed for
displaying images within Tkinter applications. See
the author's companion document for PIL
documentation: Python Imaging Library
(PIL) quick reference.
To display a two-color image in the .xbm
format, you will need this constructor:
tk.BitmapImage(file=f
[, background=b
][, foreground=c
])
where
is
the name of the f
.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=
option to set the background to color b
, and the
optional b
foreground=
option to set the foreground to color c
. For color
specification, see Section 5.3, “Colors”.
c
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()