Controlling Display Updates (Advanced)

Usually, the visual display of a GraphWin is updated whenever any graphics object's visible state is changed in some way. However, under some circumstances, for example when using the graphics library inside some interactive shells, it may be necessary to force the window to update in order for changes to be seen. The update() function is provided to do this.

update()
Causes any pending graphics operations to be carried out and the results displayed.

For efficiency reasons, it is sometimes desirable to turn off the automatic updating of a window every time one of the objects changes. For example, in an animation, you might want to change the appearance of multiple objects before showing the next ``frame'' of the animation. The GraphWin constructor includes a special extra parameter called autoflush that controls this automatic updating. By default, autoflush is on when a window is created. To turn it off, the autoflush parameter should be set to False, like this:

win = GraphWin("My Animation", 400, 400, autoflush=False)
Now changes to the objects in win will only be shown when the graphics system has some idle time or when the changes are forced by a call to update().

The update() method also takes an optional parameter that specifies the maximum rate (per second) at which updates can happen. This is useful for controlling the speed of animations in a hardware-independent fashion. For example, placing the command update(30) at the bottom of a loop ensures that the loop will ``spin'' at most 30 times per second. The update command will insert an appropriate pause each time through to maintain a relatively constant rate. Of course, the rate throttling will only work when the body of the loop itself executes in less than 1/30th of a second.


Example: 1000 frames at 30 frames per second
    win = GraphWin("Update Example", 320, 200, autoflush=False)
    for i in range(1000):
        # <drawing commands for ith frame>
        update(30)
zelle 2016-08-26