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

(yzsuai) #1

if file:
self.canvas.postscript(file=file) # save canvas to file


def help(self):
self.setTextInfo(helpstr)
#showinfo('PyDraw', helpstr)


def setTextInfo(self, text):
self.canvas.dchars(self.textInfo, 0, END)
self.canvas.insert(self.textInfo, 0, text)
self.canvas.tkraise(self.textInfo)


def onQuit(self):
if self.moving:
self.setTextInfo("Can't quit while move in progress")
else:
self.realquit() # std wm delete: err msg if move in progress


def traceEvent(label, event, fullTrace=True):
print(label)
if fullTrace:
for atrr in dir(event):
if attr[:2] != '__':
print(attr, '=>', getattr(event, attr))


if name == 'main':
from sys import argv # when this file is executed
if len(argv) == 2: PicDir = argv[1] # '..' fails if run elsewhere
root = Tk() # make, run a MovingPics object
MovingPics(root)
root.mainloop()


As is, only one object can be in motion at a time—requesting an object move while one
is already in motion pauses the original till the new move is finished. Just as in Chap-
ter 9 ’s canvasDraw examples, though, we can add support for moving more than one
object at the same time with either after scheduled callback events or threads.


Example 11-9 shows a MovingPics subclass that codes the necessary customizations to
do parallel moves with after events. It allows any number of objects in the canvas,
including pictures, to be moving independently at once. Run this file directly to see the
difference; I could try to capture the notion of multiple objects in motion with a screen-
shot, but I would almost certainly fail.


Example 11-9. PP4E\Gui\MovingPics\movingpics_after.py


"""
PyDraw-after: simple canvas paint program and object mover/animator
use widget.after scheduled events to implement object move loops, such
that more than one can be in motion at once without having to use threads;
this does moves in parallel, but seems to be slower than time.sleep version;
see also canvasDraw in Tour: builds and passes the incX/incY list at once:
here, would be allmoves = ([(incrX, 0)] reptX) + ([(0, incrY)] reptY)
"""


744 | Chapter 11: Complete GUI Programs

Free download pdf