The Canvas Widget
Class Canvas is a
powerful, flexible widget used for many purposes, including plotting
and, in particular, building custom widgets. Building custom widgets
is an advanced topic, and I do not cover it further in this book.
This section covers only a subset of Canvas
functionality used for the simplest kind of
plotting.
Coordinates within a Canvas instance
c are in pixels, with the origin at the
upper left corner of c and positive
coordinates growing rightward and downward. There are advanced
methods that let you change
c’s coordinate system,
but I do not cover them in this book.
What you draw on a Canvas instance
c are canvas items, which can be lines,
polygons, Tkinter images, arcs, ovals, texts, and
others. Each item has an item
handle by which you can refer to the item. You
can also assign symbolic names called tags to
sets of canvas items (the sets of items with different tags can
overlap). ALL is a predefined tag that applies to
all items; CURRENT is a predefined tag that
applies to the item under the mouse pointer.
Tags on a Canvas instance are different from tags
on a Text instance. The canvas tags are nothing
more than sets of items with no independent existence. When you
perform any operation, passing a Canvas tag as the
item identifier, the operation occurs on those items that are in the
tag’s current set. It makes no difference if items
are later removed from or added to that tag’s set.
You create a canvas item by calling on c a method with a name of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access