Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time as time
- from graphics import *
- class Button:
- def __init__(self, win, center, width, height, label):
- 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
- p5 = Point(self.xmin, self.ymin)
- p6 = Point(self.xmax, self.ymax)
- self.rect = Rectangle(p5, p6)
- self.rect.setFill("lightgrey")
- 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"
- if p is None:
- return False
- return(self.active and self.xmin <= p.getX() <= self.xmax and self.ymin <= p.getY() <= self.ymax)
- def getLabel(self):
- "Returns the label string on this button."
- return self.label.getText()
- 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 main():
- print("Sara Heedy, Chemical Engineering" )
- # window information
- win = GraphWin("Traffic Lights", 800, 600)
- win.setCoords(0.0, 0.0, 4.0, 6.0)
- # Start and Quit Buttons
- startButton = Button(win, Point(0.50, 0.50), 0.5, 0.5, "Start")
- startButton.activate()
- quitButton = Button(win, Point(1.50, 0.50), 0.5, 0.5, "Quit")
- # Entry Times and Boxes
- Text(Point(3.25, 5.75), "Enter Red Time (s)").draw(win)
- red = Entry(Point(3.25, 5.25), 6)
- red.setText("1.0")
- red.draw(win)
- redr = Rectangle(Point(3.00, 5.00), Point(3.50, 5.50))
- redr.setOutline("red")
- redr.draw(win)
- Text(Point(3.25, 4.75), "Enter Green Time (s)").draw(win)
- green = Entry(Point(3.25, 4.25), 6)
- green.setText("1.0")
- green.draw(win)
- greenr = Rectangle(Point(3.00, 4.00), Point(3.50, 4.50))
- greenr.setOutline("green")
- greenr.draw(win)
- Text(Point(3.25, 3.75), "Enter Yellow Time (s)").draw(win)
- yellow = Entry(Point(3.25, 3.25), 6)
- yellow.setText("1.0")
- yellow.draw(win)
- yellowr = Rectangle(Point(3.00, 3.00), Point(3.50, 3.50))
- yellowr.setOutline("yellow")
- yellowr.draw(win)
- # Pedestrian
- Text(Point(3.25, 2.75), "Enter Pedestrian, 1 or 0").draw(win)
- ped = Entry(Point(3.25, 2.25), 6)
- ped.setText("0.0")
- ped.draw(win)
- pedr = Rectangle(Point(3.00, 2.00), Point(3.50, 2.50))
- pedr.setOutline("cyan")
- pedr.draw(win)
- # Begin clicks for Traffic Lights
- # Traffic Light Title
- title = Text(Point(1.5,5.5),"Traffic Lights")
- title.setStyle("bold")
- title.setTextColor("darkgreen")
- title.setSize(18)
- title.draw(win)
- # Traffic Light 1
- message = Text(Point(1.5,5.1), "Click 2 times to make your first traffic box")
- message.draw(win)
- p1 = win.getMouse()
- p1.draw(win)
- p2 = win.getMouse()
- p2.draw(win)
- box1 = Rectangle(p1,p2)
- box1.setOutline("black")
- box1.setFill("grey")
- box1.draw(win)
- message.setText("Click 3 times to make traffic lights")
- a1 = win.getMouse()
- circ1 = Circle(a1,.2)
- circ1.setOutline("black")
- circ1.setFill("red")
- circ1.draw(win)
- a2 = win.getMouse()
- circ2 = Circle(a2,.2)
- circ2.setOutline("black")
- circ2.setFill("yellow")
- circ2.draw(win)
- a3 = win.getMouse()
- circ3 = Circle(a3,.2)
- circ3.setOutline("black")
- circ3.setFill("green")
- circ3.draw(win)
- # Traffic Light 2
- message.setText("Click 2 times to make second traffic box")
- p3 = win.getMouse()
- p3.draw(win)
- p4 = win.getMouse()
- p4.draw(win)
- box2 = Rectangle(p3,p4)
- box2.setOutline("black")
- box2.setFill("grey")
- box2.draw(win)
- message.setText("Click 3 times to make traffic lights")
- a4 = win.getMouse()
- circ4 = Circle(a4,.2)
- circ4.setOutline("black")
- circ4.setFill("red")
- circ4.draw(win)
- a5 = win.getMouse()
- circ5 = Circle(a5,.2)
- circ5.setOutline("black")
- circ5.setFill("yellow")
- circ5.draw(win)
- a6 = win.getMouse()
- circ6 = Circle(a6,.2)
- circ6.setOutline("black")
- circ6.setFill("green")
- circ6.draw(win)
- # Both traffic lights are made
- message.setText("Both traffic lights are made, please set conditions and click 'Start'.")
- circ1.setFill("black")
- circ2.setFill("black")
- circ3.setFill("black")
- circ4.setFill("black")
- circ5.setFill("black")
- circ6.setFill("black")
- # Loop
- pt = win.getMouse()
- quitButton.activate()
- while not quitButton.clicked(pt):
- pt = win.checkMouse()
- if startButton.clicked(pt):
- message.setText("Traffic program has started.")
- while eval(ped.getText()) == 1.0: # Check pedestrians
- circ1.setFill("red")
- circ4.setFill("red")
- ped.setText("0.0")
- time.sleep(eval(red.getText()))
- break
- # Red and green light combo, check which is greater time interval
- if eval(red.getText()) <= eval(green.getText()):
- circ3.setFill("black")
- circ2.setFill("black")
- circ4.setFill("black")
- circ1.setFill("red")
- circ6.setFill("green")
- time.sleep(eval(green.getText()))
- else:
- circ3.setFill("black")
- circ2.setFill("black")
- circ4.setFill("black")
- circ1.setFill("red")
- circ6.setFill("green")
- time.sleep(eval(red.getText()))
- # Red and yellow light combo, check which is greater time interval
- if eval(red.getText()) <= eval(yellow.getText()):
- circ6.setFill("black")
- circ5.setFill("yellow")
- time.sleep(eval(yellow.getText()))
- else:
- circ6.setFill("black")
- circ5.setFill("yellow")
- time.sleep(eval(red.getText()))
- # Red and green light combo, check which is greater time interval
- if eval(red.getText()) <= eval(green.getText()):
- circ5.setFill("black")
- circ1.setFill("black")
- circ4.setFill("red")
- circ3.setFill("green")
- time.sleep(eval(green.getText()))
- else:
- circ5.setFill("black")
- circ1.setFill("black")
- circ4.setFill("red")
- circ3.setFill("green")
- time.sleep(eval(red.getText()))
- # Red and yellow light combo, check which is greater time interval
- if eval(red.getText()) <= eval(yellow.getText()):
- circ3.setFill("black")
- circ2.setFill("yellow")
- time.sleep(eval(yellow.getText()))
- else:
- circ3.setFill("black")
- circ2.setFill("yellow")
- time.sleep(eval(red.getText()))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement