from movingpics import *
class MovingPicsAfter(MovingPics):
def doMoves(self, delay, objectId, incrX, reptX, incrY, reptY):
if reptX:
self.canvas.move(objectId, incrX, 0)
reptX -= 1
else:
self.canvas.move(objectId, 0, incrY)
reptY -= 1
if not (reptX or reptY):
self.moving.remove(objectId)
else:
self.canvas.after(delay,
self.doMoves, delay, objectId, incrX, reptX, incrY, reptY)
def onMove(self, event):
traceEvent('onMove', event, 0)
object = self.object # move cur obj to click spot
if object:
msecs = int(pickDelays[0] * 1000)
parms = 'Delay=%d msec, Units=%d' % (msecs, pickUnits[0])
self.setTextInfo(parms)
self.moving.append(object)
incrX, reptX, incrY, reptY = self.plotMoves(event)
self.doMoves(msecs, object, incrX, reptX, incrY, reptY)
self.where = event
if name == 'main':
from sys import argv # when this file is executed
if len(argv) == 2:
import movingpics # not this module's global
movingpics.PicDir = argv[1] # and from* doesn't link names
root = Tk()
MovingPicsAfter(root)
root.mainloop()
To appreciate its operation, open this script’s window full screen and create some ob-
jects in its canvas by pressing “p” after an initial click to insert pictures, dragging out
shapes, and so on. Now, while one or more moves are in progress, you can start another
by middle-clicking on another object and right-clicking on the spot to which you want
it to move. It starts its journey immediately, even if other objects are in motion. Each
object’s scheduled after events are added to the same event loop queue and dispatched
by tkinter as soon as possible after a timer expiration.
If you run this subclass module directly, you might notice that movement isn’t quite
as fast or as smooth as in the original (depending on your machine, and the many layers
of software under Python), but multiple moves can overlap in time.
Example 11-10 shows how to achieve such parallelism with threads. This process
works, but as we learned in Chapters 9 and 10, updating GUIs in spawned threads is
generally a dangerous affair. On one of my machines, the movement that this script
implements with threads was a bit jerkier than the original version—perhaps a
PyDraw: Painting and Moving Graphics| 745