1. button.py – a Button
class
# button.py
from
graphics import *
class
button:
"""A
button isa labeled rectangle ina
window.
It is activated or
deactivated with the activate()
and deactivate()
methods. The clicked(p) method
returns True if the
button is active and p is inside it."""
def __init__(self,
win, center, width, height, label):
"""
Creates a rectangular button eq:
qb = Button(myWin,
centerPoint, width, height, 'Quit')"""
w, h = width/2.0, height/2.0
x, y = center.getX(), center.getY()
self.xmax , self.xmin = x+w,
x-w
self.ymax , self.ymin = y+h,
y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1, p2)
self.rect.setFill('lightgray')
self.rect.draw(win)
self.label = Text(center, label)
self.label.draw(win)
self.deactivate();
def clicked(self,
p):
"Returns
true if button active and p is inside"
return self.active
and \
self.xmin
<= p.getX() <= self.xmax and \
self.ymin
<= p.getY() <= self.ymax
def getLabel(self):
"Returns
the label string of this button"
return self.label.getText()
def activate(self):
"Sets the
button to 'active'"
self.label.setFill('black')
self.rect.setWidth(2)
self.active =
True
def deactivate(self):
"Sets this
button to 'inactive'"
self.label.setFill('darkgrey')
self.rect.setWidth(1)
self.active =
False
2. dieview
– A Six-Sided Die Graphical Object
# dieview.py
from graphics import *
from random import randrange
class DieView:
""" DieView
is a widget that displays a graphical representation
or a standard six-sided
die"""
def
__init__(self, win, center, size):
"""Create a view of a die,
e.g.:
d1 = DieView(myWin,
Point(40, 50), 20)
creates a die centered at (40, 50)
having sides
of length 20"""
# first define some standard values
self.win = win
self.background = "red" # color of face
self.foreground = "white" # color of
pips
self.psize = 0.1 * size # radius of each pip
hsize = size / 2.0 # half size of die
offset = 0.6 * hsize # distance from center to outer pip
# create
a square for the face
cx, cy = center.getX(), center.getY()
p1 = Point(cx-hsize,
cy-hsize)
p2 = Point(cx+hsize, cy+hsize)
rect = Rectangle(p1,
p2)
rect.draw(win)
rect.setFill(self.background)
# create 7 circles for standard pip
locations
self.pip1 = self.__makePip(cx-offset, cy-offset)
self.pip2 = self.__makePip(cx-offset, cy)
self.pip3 = self.__makePip(cx-offset, cy+offset)
self.pip4 = self.__makePip(cx, cy)
self.pip5 = self.__makePip(cx+offset, cy-offset)
self.pip6 = self.__makePip(cx+offset, cy)
self.pip7 = self.__makePip(cx+offset, cy+offset)
# draw an initial value
self.setValue(randrange(1,7))
def
__makePip(self,
x, y):
"Internal helper method to draw a pip at (x,y)"
pip = Circle(Point(x,y),
self.psize)
pip.setFill(self.background)
pip.setOutline(self.background)
pip.draw(self.win)
return pip
def
setValue(self,
value):
"Set this die to display value"
self.pip1.setFill(self.background)
self.pip2.setFill(self.background)
self.pip3.setFill(self.background)
self.pip4.setFill(self.background)
self.pip5.setFill(self.background)
self.pip6.setFill(self.background)
self.pip7.setFill(self.background)
# set
correct pips on
if (value == 1):
self.pip4.setFill(self.foreground)
elif (value == 2):
self.pip1.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif (value == 3):
self.pip1.setFill(self.foreground)
self.pip7.setFill(self.foreground)
self.pip4.setFill(self.foreground)
elif (value == 4):
self.pip1.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif (value == 5):
self.pip1.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip4.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif (value == 6):
self.pip1.setFill(self.foreground)
self.pip2.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip6.setFill(self.foreground)
self.pip7.setFill(self.foreground)
3. rational.py – A
Rational Number Class
# rational.py
import string
class Rational:
def
__init__(self, a, b):
if (b < 0):
a , b = -a, -b
self.n, self.d = self.__reduce(a,
b)
def
numerator(self):
return self.n
def
denominator(self):
return self.d
def
__reduce(self, a, b):
# helper
function to reduce to lowest terms
a0, b0 = a, b # make copies
while (a % b != 0):
a, b, = b, a % b
return a0/b, b0/b
def
r_print(self):
print "%d/%d" % (self.n, self.d)
def
r_format(self):
“ returns
rational formatted to be printed as a string”
return str(self.n)+"/"+str(self.d)
def
__str__(self):
“ returns rational formatted as a
string”
return str(self.n)+"/"+str(self.d)
def r_input(prompt):
x =
raw_input(prompt)
n,
d = string.split(x,"/")
return Rational(int(n),
int(d))
def r_plus(a, b):
d =
a.denominator() * b.denominator()
n =
a.numerator()*b.denominator() + b.numerator()*a.denominator()
return Rational(n, d)
def r_minus(a, b):
d =
a.denominator() * b.denominator()
n =
a.numerator()*b.denominator() - b.numerator()*a.denominator()
return Rational(n, d)
def r_times(a, b):
n =
a.numerator() * b.numerator()
d =
a.denominator() * b.denominator()
return Rational(n, d)
def r_slash(a, b):
n =
a.numerator() * b.denominator()
d =
b.numerator() * a.denominator()
return Rational(n, d)