When run, this script draws the window captured in Figure 9-23. We saw how to place
a photo on canvas and size a canvas for a photo earlier on this tour (see “Im-
ages” on page 484). This script also draws shapes, text, and even an embedded Label
widget. Its window gets by on looks alone; in a moment, we’ll learn how to add event
callbacks that let users interact with drawn items.
Figure 9-23. canvas1 hardcoded object sketches
Programming the Canvas Widget
Canvases are easy to use, but they rely on a coordinate system, define unique drawing
methods, and name objects by identifier or tag. This section introduces these core
canvas concepts.
Coordinates
All items drawn on a canvas are distinct objects, but they are not really widgets. If you
study the canvas1 script closely, you’ll notice that canvases are created and packed (or
gridded or placed) within their parent container just like any other widget in tkinter.
But the items drawn on a canvas are not. Shapes, images, and so on, are positioned and
moved on the canvas by coordinates, identifiers, and tags. Of these, coordinates are
the most fundamental part of the canvas model.
Canvases define an (X,Y) coordinate system for their drawing area; x means the hori-
zontal scale, y means vertical. By default, coordinates are measured in screen pixels
(dots), the upper-left corner of the canvas has coordinates (0,0), and x and y coordinates
Canvas | 551