Setting up a subdirectory for week 5 in-class work
Thursday: If you copied over all of my week05 files already (Tuesday
instructions below), then I want you to copy over a new version of the
duplicate.py file (and enter y when cp asks if you want to overwrite it).
Otherwise, follow Tuesday instructions:
$ cd cs21/class/week05
$ pwd
$ cp ~newhall/public/cs21/week05/duplicate.py .
cp: overwrite './duplicate.py'? y
Tuesday:
cd into your cs21/class/ subdirectory and create a new directory named 'week05',
then cd into that directory:
$ cd cs21/class # cd into your cs21/class subdirectory
$ pwd
/home/your_user_name/cs21/class
$ mkdir week05 # make a subdirectory named week05
$ cd week05 # cd into it
$ pwd
/home/your_user_name/cs21/class/week05
Now copy over all the files from my public/cs21/week05 directory into your
week05 directory (remember to add the dot as the destination of the cp command).
From your week05 directory:
$ cp ~newhall/public/cs21/week05/* .
$ ls
animate.py circleshift.py partsmove.py squares_list.py
bullseye.py duplicate.py randomcircle.py test_graphics.py
Weekly In-class Examples
This week we are going to write some programs that manipulate objects. We
will primarily be using the graphics library to create graphics objects
that can be drawn into a graphics window.
The graphics library
To use the graphics library, first import it at the top of your program:
from graphics import *
Next create a new graphics window object, and then create gui objects
to draw into this window:
# creates new GraphWin object, 500x500 pixels in size
win = GraphWin("My GUI Program", 500, 500)
# creates a new Circle object centered at 50,50 with a radius of 20 pixels
circ = Circle(Point(50,50), 20)
# invoke the setFill method of the Circle object referred to by circ
circ.setFill("red")
# draw the Circle object refered to by circ in GraphWin win
circ.draw(win)
Some more notes on using the Graphics library are available
here: Graphics Library Reference. A link to this is also available in the References
section at the bottom of the class webpage.
We are going to do some of the following together in class:
- open squares_list.py. We will complete the function from last
week that takes a list of values and returns the square of the largest.
We will also look at some examples of using the object interface to
a list by creating a list of items using the append method.
- open test_graphics.py in vim. It contains some example code that
draws different type objects to one of two graph windows. Let's try
changing some things.
- open bullseye.py. Together we are going to write a program that
draws a bullseye to the graphics window. It should consist of 4
concentric circles, of two or more colors, that are drawn centered
in the graphics window.
- open randomcircle.py. Together we are going to write a function
that draws some number of circles to random locations in the
graphics window.
- Run colorPicker in the python interpreter to see the
different available colors:
$ python
from colorPicker import *
colorPicker()
- open circle_shift.py. Try running it in python. Together we
will change this program so that after first drawing the initial
cirle, each time the user clicks on a new spot, the circle is
shifted to the new location. The shifting will happen four times,
and on the fifth mouse click the program will exit.
- open duplicate.py. We are going to try to use an existing
Circle object as a starting point for creating a similar second
Circle object. The attempt at doing this doesn't work. Let's
think about why not and how to fix it.
- open animate.py. Let's look at this program together.
Once we figure out what it is doing, lets change the code
so that the animation will be repeated 3 times, each end point will
be determined by the user's mouse click. If we want to repeat the
same action multiple times, what language construct do we use? ...