[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

increase to the right and down, respectively. To draw and embed objects within a can-
vas, you supply one or more (X,Y) coordinate pairs to give absolute canvas locations.
This is different from the constraints we’ve used to pack widgets thus far, but it allows
very fine-grained control over graphical layouts, and it supports more free-form inter-
face techniques such as animation.*


Object construction


The canvas allows you to draw and display common shapes such as lines, ovals, rec-
tangles, arcs, and polygons. In addition, you can embed text, images, and other kinds
of tkinter widgets such as labels and buttons. The canvas1 script demonstrates all the
basic graphic object constructor calls; to each, you pass one or more sets of (X,Y) co-
ordinates to give the new object’s location, start point and endpoint, or diagonally
opposite corners of a bounding box that encloses the shape:


id = canvas.create_line(fromX, fromY, toX, toY) # line start, stop
id = canvas.create_oval(fromX, fromY, toX, toY) # two opposite box corners
id = canvas.create_arc( fromX, fromY, toX, toY) # two opposite oval corners
id = canvas.create_rectangle(fromX, fromY, toX, toY) # two opposite corners

Other drawing calls specify just one (X,Y) pair, to give the location of the object’s upper-
left corner:


id = canvas.create_image(250, 0, image=photo, anchor=NW) # embed a photo
id = canvas.create_window(100, 100, window=widget) # embed a widget
id = canvas.create_text(100, 280, text='Ham') # draw some text

The canvas also provides a create_polygon method that accepts an arbitrary set of
coordinate arguments defining the endpoints of connected lines; it’s useful for drawing
more arbitrary kinds of shapes composed of straight lines.


In addition to coordinates, most of these drawing calls let you specify common con-
figuration options, such as outline width, fill color, outline color, and so on. Indi-
vidual object types have unique configuration options all their own, too; for instance,
lines may specify the shape of an optional arrow, and text, widgets, and images may
be anchored to a point of the compass (this looks like the packer’s anchor, but really it
gives a point on the object that is positioned at the [X,Y] coordinates given in the
create call; NW puts the upper-left corner at [X,Y]).


Perhaps the most important thing to notice here, though, is that tkinter does most of
the “grunt” work for you—when drawing graphics, you provide coordinates, and
shapes are automatically plotted and rendered in the pixel world. If you’ve ever done
any lower-level graphics work, you’ll appreciate the difference.



  • Animation techniques are covered at the end of this tour. As a use case example, because you can embed
    other widgets in a canvas’s drawing area, their coordinate system also makes them ideal for implementing
    GUIs that let users design other GUIs by dragging embedded widgets around on the canvas—a useful canvas
    application we would explore in this book if I had a few hundred pages to spare.


552 | Chapter 9: A tkinter Tour, Part 2

Free download pdf