the script uses the canvas find_closest method to fetch the object ID of the particular
text item clicked (the one closest to the click spot):
C:\...\PP4E\Gui\Tour> python canvas-bind.py
Got canvas click 3 6 .8217952 canvas clicks
Got canvas click 46 52 .8217952
Got object click 51 33 .8217952 (1,) first text click
Got canvas click 51 33 .8217952
Got object click 55 69 .8217952 (2,) second text click
Got canvas click 55 69 .8217952
We’ll revisit the notion of events bound to canvases in the PyDraw example in Chap-
ter 11, where we’ll use them to implement a feature-rich paint and motion program.
We’ll also return to the canvasDraw script later in this chapter, to add tag-based moves
and simple animation with time-based tools, so keep this page bookmarked for refer-
ence. First, though, let’s follow a promising side road to explore another way to lay out
widgets within windows—the gridding layout model.
Grids
So far, we’ve mostly been arranging widgets in displays by calling their pack methods—
an interface to the packer geometry manager in tkinter. We’ve also used absolute co-
ordinates in canvases, which are a kind of layout scheme, too, but not a high-level
managed one like the packer. This section introduces grid, the most commonly used
alternative to the packer. We previewed this alternative in Chapter 8 when discussing
input forms and arranging image thumbnails. Here, we’ll study gridding in its full form.
As we learned earlier, tkinter geometry managers work by arranging child widgets
within a parent container widget (parents are typically Frames or top-level windows).
When we ask a widget to pack or grid itself, we’re really asking its parent to place it
among its siblings. With pack, we provide constraints or sides and let the geometry
manager lay out widgets appropriately. With grid, we arrange widgets in rows and
columns in their parent, as though the parent container widget was a table.
Gridding is an entirely distinct geometry management system in tkinter. In fact, at this
writing, pack and grid are mutually exclusive for widgets that have the same parent—
within a given parent container, we can either pack widgets or grid them, but we cannot
do both. That makes sense, if you realize that geometry managers do their jobs as
parents, and a widget can be arranged by only one geometry manager.
Why Grids?
At least within one container, though, that means you must pick either grid or pack
and stick with it. So why grid, then? In general, grid is handy for displays in which
otherwise unrelated widgets must line up horizontally. This includes both tabular dis-
plays and form-like displays; arranging input fields in row/column grid fashion can be
at least as easy as laying out the display with nested frames.
564 | Chapter 9: A tkinter Tour, Part 2