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

(yzsuai) #1
mouse location, you need a starting point; to delete before a redraw, you also must
remember the last drawn object’s identifier. Two events come into play: the initial
button press event saves the start coordinates (really, the initial press event object,
which contains the start coordinates), and mouse movement events erase and re-
draw from the start coordinates to the new mouse coordinates and save the new
object ID for the next event’s erase.

Object moves
When you click the right mouse button (button 3), the script moves the most
recently drawn object to the spot you clicked in a single step. The event argument
gives the (X,Y) coordinates of the spot clicked, and we subtract the saved starting
coordinates of the last drawn object to get the (X,Y) offsets to pass to the canvas
move method (again, move does not take positions). Remember to scale event coor-
dinates first if your canvas is scrolled.


The net result creates a window like that shown in Figure 9-27 after user interaction.
As you drag out objects, the script alternates between ovals and rectangles; set the
script’s trace global to watch object identifiers scroll on stdout as new objects are drawn
during a drag. This screenshot was taken after a few object drag-outs and moves, but
you’d never tell from looking at it; run this example on your own computer to get a
better feel for the operations it supports.


Figure 9-27. canvasDraw after a few drags and moves


562 | Chapter 9: A tkinter Tour, Part 2

Free download pdf