Matthew L. Wright
Assistant Professor, St. Olaf College

Homework 23: Interactive Graphics Exercise

CS 121 ⋅ Spring 2016

Upload your solutions to the following questions to Moodle for HW23.

This homework requires the Python graphics module. To use the Python graphics module on your personal computer, you will need to download You might also want to refer to the Python graphics tutorial and the Python graphics documentation.

  1. Use Python graphics to write a program that draws a blue square, and when the user clicks on the square it turns red. If the user clicks outside of the square, nothing should happen.

    Hint: You will need to use the getMouse() method of GraphWin. This method returns a Point, whose coordinates are where the user clicked. For example:

    pt = win.getMouse() #pt is the Point where the user clicked
  2. Modify your program so that the user can click the square many times, and each time the square turns a random color. The random color should be generated each time the user clicks the square.

    Hint: The setFill() method accepts an arbitrary color specified in terms of its red, green, and blue components, as follows:

    rect.setFill(color_rgb(red, green, blue))

    Each of the components red, green, and blue is specified by an integer between 0 and 255. The graphics module provides the function color_rgb(), which converts the red, green, and blue values into a color string that setFill() understands.