3

Python Question

view full story
linux-howto

http://www.daniweb.com – #face1.py from face import Face from graphics import * from button import Button def main (): win = GraphWin ("Faces",500,500) win.setBackground ("yellow") f1= Face (win, Point (250,250), 70) #f2= f1.smile(win,Point(250,250), 70) #f3= f1.frown(win,Point(250,250), 70) rollButton = Button (win, Point (400, 250), 55, 30, "Grim") rollButton2 = Button (win, Point (250, 100), 55, 30, "Smiley") rollButton3 = Button (win, Point (50, 250), 55, 30, "Frown") rollButton.activate () rollButton2.activate () rollButton3.activate () quitButton = Button (win, Point (250, 450), 55, 30, "Quit") pt = win.getMouse () while not quitButton.clicked (pt):'''Here is where the problem is, when I run the program, the windows freezes''' if rollButton.clicked (pt): f1= Face (win, Point (250,250), 70) update() elif rollButton2.clicked (pt): f2= f1.smile(win,Point(250,250), 70) update() elif rollButton3.clicked (pt): f3= f1.frown(win,Point(250,250), 70) update() else: quitButton.activate () pt = win.getMouse () # Now it is time to quit win.close () #face.py from graphics import * class Face: def __init__ (self, window, center, size): eyeSize = 0.15 * size eyeOff = size / 3.0 mouthSize = 0.5 * size mouthOff= size/ 3.0 self.head = Circle (center, size) self.head.setFill("beige") self.head.draw(window) self.leftEye = Circle (center, eyeSize) self.leftEye.setFill("black") self.leftEye.move (-eyeOff, -eyeOff) self.rightEye = Circle (center, eyeSize) self.rightEye.setFill("black") self.rightEye.move (eyeOff, -eyeOff) self.leftEye.draw(window) self.rightEye.draw(window) p1=center.clone() p1.move (-mouthSize/2, mouthOff) p2 = center.clone() p2.move(mouthSize/2, mouthOff) self.mouth = Line (p1,p2) self.mouth.draw (window) def smile(self,window,center,size): eyeSize = 0.15 * size eyeOff = size / 3.0 self.head = Circle (center, size) self.head.setFill("green") self.head.draw(window) Point1 = self.head.getCenter() Point2 = size * .20 Point3 = size * .30 Point4 = size * .40 Point5 = size * .25 self.leftEye = Circle (center, eyeSize) self.leftEye.setFill("black") self.leftEye.move (-eyeOff, -eyeOff) self.rightEye = Circle (center, eyeSize) self.rightEye.setFill("black") self.rightEye.move (eyeOff, -eyeOff) self.leftEye.draw(window) self.rightEye.draw(window) mouth = Circle(Point1, Point3) mouth.move(0, Point4) mouth.draw(window) mouth2 = Circle(Point1, Point3) mouth2.setFill("beige") mouth2.setOutline('beige') mouth2.move(0, Point5) mouth2.draw(window) def frown(self,window, center, size): eyeSize = 0.15 * size eyeOff = size / 3.0 mouthSize = 0.5 * size mouthOff= size/ 3.0 self.head = Circle (center, size) self.head.setFill("brown") self.head.draw(window) Point1 = self.head.getCenter() Point2 = size * .20 Point3 = size * .30 Point4 = size * .40 Point5 = size * .25 self.leftEye = Circle (center, eyeSize) self.leftEye.setFill("black") self.leftEye.move (-eyeOff, -eyeOff) self.rightEye = Circle (center, eyeSize) self.rightEye.setFill("black") self.rightEye.move (eyeOff, -eyeOff) self.leftEye.draw(window) self.rightEye.draw(window) mouth = Circle(Point1, -Point3) mouth.move(0, Point5) mouth.draw(window) mouth2 = Circle(Point1, -Point3) mouth2.setFill("brown") mouth2.setOutline('brown') mouth2.move(0, Point4) mouth2.draw(window) # button.py from graphics import * class Button: """A button is a labeled rectangle in a window. It is activated or deactivated with the activate () and deactivated () 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. e.g.: qb = Button (myWin, centerPoint, width, height, 'Quit')""" w2 = width / 2.0 h2 = height / 2.0 x = center.getX () y = center.getY () self.xmin = x - w2 self.xmax = x + w2 self.ymin = y - h2 self.ymax = y + h2 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 activate (self): """Sets this 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 def getLabel (self): """Returns the label string of this button.""" return self.label.getText () 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 main (): win = GraphWin ('Button', 500, 400) qb = Button (win, Point (250, 200), 100, 50, 'Quit') qb.activate () eb = Button (win, Point (100, 100), 100, 50, 'Exit') I doing a project that add methods to the face class(face.py), that cause the face to change the expression. Well, my problem is that when I run the face1.py, the window freezes and I need to restart the program. I think the problem is in the face1.py. Can anyone help me. I added a break statement and the face changed, but it closed the window. (General)