Drawing in "Slow motion".

Fredrik Lundh fredrik at pythonware.com
Tue Feb 12 16:42:15 CET 2002

Eric Brunel wrote:
> - keep your time.sleep calls, but call the update_idletasks method on the
> canvas after each sleep. The call should redraw the screen.
> - a bit trickier: use the delayed callback facility in Tk via the "after"
> method to draw while your application is running. This is a bit more
> complicated and you would certainly have to rewrite your code differently.

if you're using 2.2, you can replace the call to time.sleep() with a
yield, and add a small wrapper like the "func" function below:

from __future__ import generators

import random
from Tkinter import *

w, h = 500, 500

root = Tk()
cnv = Canvas(root, width=w, height=h)
Button(root, text='Enough', command=root.quit).pack(side=TOP)

def draw():
    x, y = random.randrange(w), random.randrange(h)
    while 1:
        oldx, oldy = x, y
        x, y = random.randrange(w), random.randrange(h)
        cnv.create_line(oldx, oldy, x, y)
        yield 500 # sleep

def func(iter):
    root.after(iter.next(), func, iter)

func(draw()) # get things going



<!-- (the eff-bot guide to) the python standard library:

More information about the Python-list mailing list