Exercise 12.1

First, make sure that you can run the circle.py code, then do the following.

  1. Draw a circle in the middle of the canvas.
  2. Give the circle a random color.

Exercise 12.2

Add commands to animate an expanding circle, with radius starting at 0 and growing.

The pattern for animations is as follows.

while animation is running:
    Draw a frame of the animation.
    Modify the image state for the next animation frame.
    canvas.after(rate)
    canvas.update()

Exercise 12.3

Add mouse event code to start new circles. Each circle should:

  1. be centered at the spot of the mouse click.
  2. start growing again from a zero radius.
  3. get a new random color.

The pattern for handing mouse-click events is as follows.

# Put this in the main GUI constructor.
self._window.bind('<Button-1>', mouse event handler)

# Add this event handler method.
def method name(self, event):
    print("Clicked at: ", event.x, event.y)        
    Modify the state of the animation.

Exercise 12.4

Add a keypress event handler that moves the current circle up, down, right or left based on the arrow keys.

The pattern for handing key-click events is as follows.

# Put this in the main GUI constructor.
    self._window.bind('<Key>', key event handler method name)

# Add this event handler method.
def method name(self, event):
    print('Symbolic name of the key: ', event.keysym)        
    Modify the state of the animation.
				

Symbolic names include: “Left”, “Right”, “Up”, “Down”,“Return”,“space”.