Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: cp1252 -*-
- from Tkinter import *
- import time, threading, random, math
- class Application(Frame):
- def __init__(self, master=None):
- self.hora = random.uniform(6,23)
- Frame.__init__(self, master)
- self.grid()
- self.lienzo = Canvas(width=560, height=560)
- self.draw()
- def draw(self):
- #Pasto
- self.lienzo.create_rectangle(0, 0,560, 560, fill ='darkGreen')
- self.lienzo.grid(row=0, column=0, rowspan=30, columnspan=10)
- #Calles
- self.lienzo.create_rectangle(200, 0, 360, 560, fill ='darkGray', outline ='darkGray') #Vertical
- self.lienzo.create_line(280, 0, 280, 560, fill ='lightGray') #Línea Vertical
- self.lienzo.create_rectangle(0, 200, 560, 360, fill ='darkGray', outline='darkGray') #Horizontal
- self.lienzo.create_line(0,280, 560,280, fill ='lightGray') #Línea Horizontal
- self.lienzo.create_rectangle(200,200, 360,360, fill ='darkGray', outline='yellow', outlinestipple='gray75', width=4) #Centro
- #Labels
- self.lienzo.create_text(220, 15, text="Alpha")
- self.lienzo.create_text(320, 545, text="Gamma")
- self.lienzo.create_text(15, 340, text="Beta")
- self.lienzo.create_text(545, 220, text="Delta")
- #LightsAlpha
- self.lienzo.create_oval(210, 180, 220, 190, fill='cyan') #AR
- self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- #LightsGamma
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
- self.lienzo.create_oval(340, 370, 350, 380, fill='cyan') #CR
- #LightsBeta
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
- self.lienzo.create_oval(180, 340, 190, 350, fill='cyan') #BR
- #LightsDelta
- self.lienzo.create_oval(370, 210, 380, 220, fill='cyan') #DR
- self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- #Pantalla
- self.screen = Text(height=30, width=25, wrap=WORD, bd=15)
- self.screen.grid(row=0, column=11, rowspan=10)
- #Botones
- self.repeatButton = Button(text="Go!", command = self.run)
- self.repeatButton.grid(row=11, column=10, columnspan=40, rowspan=40)
- self.update()
- def run(self):
- morning_ratio = morning.evaluate(app.hora)
- noon_ratio = noon.evaluate(app.hora)
- night_ratio = night.evaluate(app. hora)
- if morning_ratio > noon_ratio:
- eval_ratio = morning_ratio
- elif noon_ratio > night_ratio:
- eval_ratio = noon_ratio
- else:
- eval_ratio = night_ratio
- N = 4 + 4 * eval_ratio
- n = 2
- self.screen.delete(0.0,END)
- #Tiempo 1 -- AS+AR y CS+CR
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='green') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='green') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- self.screen.insert(END, "Son las " + str(convertToTime(self.hora)) + " Y se agrega un " + str(math.trunc(eval_ratio * 100)) + " % al reloj\nAlpha y Gamma: Puedes ir al frente y doblar a la derecha por " + str("%.2f" % N) + " segundos\n")
- #Pausa
- self.update()
- time.sleep(N)
- #Tiempo 2 -- BS+BR y DS+DR
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='green') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='green') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- self.screen.insert(END, "Beta y Delta: Puedes ir al frente y doblar a la derecha por " + str("%.2f" % N) + " segundos\n")
- #Pausa
- self.update()
- time.sleep(N)
- #Tiempo 3 -- AL+AS+AR
- self.lienzo.create_oval(238, 180, 248, 190, fill='green') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='green') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- self.screen.insert(END, "Alpha: Puedes ir al frente y doblar a la derecha e izquierda por " + str(n) + " segundos\n")
- self.update()
- time.sleep(n)
- #Tiempo 4 -- CL+CS+CR
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='green') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='green') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- self.screen.insert(END, "Gamma: Puedes ir al frente y doblar a la derecha e izquierda por " + str(n) + " segundos\n")
- self.update()
- time.sleep(n)
- #Tiempo 5 -- BL+BS+BR
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='green') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='green') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
- self.screen.insert(END, "Beta: Puedes ir al frente y doblar a la derecha e izquierda por " + str(n) + " segundos\n")
- self.update()
- time.sleep(n)
- #Tiempo 6 -- DL+DS+DR
- self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
- self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
- self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
- self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
- self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
- self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
- self.lienzo.create_oval(370, 224, 380, 234, fill='green') #DS
- self.lienzo.create_oval(370, 238, 380, 248, fill='green') #DL
- self.screen.insert(END, "Delta: Puedes ir al frente y doblar a la derecha e izquierda por " + str(n) + " segundos\n")
- self.update()
- time.sleep(n)
- self.hora += 0.5
- class mf(object):
- def __init__(self, p):
- self._p = p[:]
- class trapecio(mf):
- def evaluate(self,x):
- if x<=self._p[0]:
- return 0
- elif self._p[0] < x <= self._p[1]:
- return (x - self._p[0])/(self._p[1] - self._p[0])
- elif self._p[1] < x <= self._p[2]:
- return 1
- elif self._p[2] < x <= self._p[3]:
- return 1 - (x - self._p[2])/(self._p[3] - self._p[2])
- else:
- return 0
- def convertToTime(tiempo):
- hr = int(math.floor(tiempo))
- mins = int(math.trunc((tiempo - hr) *60))
- if mins < 10:
- return "%d:0%d" % (hr, mins)
- else:
- return "%d:%d" % (hr, mins)
- mainwin = Tk()
- mainwin.title("Semáforo v5.2")
- mainwin.geometry("800x560")
- app = Application()
- app.update()
- morning = trapecio([5, 7, 9, 11])
- noon = trapecio([11, 14, 16, 17])
- night = trapecio([17, 19, 21,23])
- mainwin.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement