CSci 157 - Lab 5
Purpose: the main objective is to learn how to use selection statements and the graphics module.
Warmup Questions. We'll work through these together; you don't need to write these out.
- What method(s) might you use to change the size or font of a Text object?
- How can we display images (for example, photos) in a program?
- How do we determine when the mouse is clicked on a specific region of the canvas, or in a graphical object such as a Circle or Rectangle?
Part I. Create a program vote.py which will use mouse clicks to determine which of two candidates the user of the program prefers. Candidates don't have to be politicians; you could be choosing the favorite of two songs, dogs vs. cats, Coke vs. Pepsi, Sewanee eateries, Super Bowl teams...whatever.
The program will have the following features:
- At the beginning:
- Use a Line object to divide the window (vertically) into two halves. The line should not extend all the way to the top or bottom of the window, we want to add some text there.
- Use a Text object to label each half with the name of your candidates. Use colors to highlight/differentiate the names, but not RED.
- Add text centered at the bottom of the window, something like: Which do you prefer?
- Wait for a mouse click
- Once you get a mouse click:
- Determine which side of the window the mouse was clicked in (for now we only care about the x-coordinate of the Point clicked)
- Change the color of the text naming the selected candidate to RED
- Change the text at the bottom to say that the selected candidate wins
- Wait until the user clicks in the window again, then close the window.
Part II. Switch driver and observer.
Modify the existing program as
described below. The primary changes are to 1) determine users's
preference by detecting clicks in an image,
and 2) add visual interest. See this program
for an example of loading and displaying images. You'll need these two
images downloaded in the same directory to run it: image1,
image2.
In order to use images, place the image files in the same directory with your program files (later we'll relax this requirement).
As noted below, only a few graphics image formats are supported (GIF, PPM, PNG all work for me).
- At the beginning:
- Add a "title" for the program somewhere near the top in large font.
- Here things get tricky. You'll want to add an image for each candidate using graphics.Image but the Image facility has some limitations. You'll need images small enough to fit in (no more than) half the window. When drawing the Images, make sure they don't cover up the title or the vote tally.
- Add the candidate's name (or whatever) as Text under the image
- Wait for a mouse click
- On mouse click:
- Determine which image the mouse was clicked in
- Change the color of the text naming the selected candidate to RED
- If the point where the mouse was clicked is inside one of the images, declare that candidate the winner as before. - if the click is in neither image, nobody wins (say that instead)
Note: If you can't get your images don't load, and you've exhausted all possibilities, don't fight with it. Comment out the image code and replace the images with a colored Rectangle and a Text label.
Demonstrate your program to the instructor.
An example from previous years:
