Advertisement
Sax

Semáforo v5.2

Sax
Feb 22nd, 2012
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.57 KB | None | 0 0
  1. # -*- coding: cp1252 -*-
  2. from Tkinter import *
  3. import time, threading, random, math
  4.  
  5. class Application(Frame):
  6.     def __init__(self, master=None):
  7.  
  8.         self.hora = random.uniform(6,23)
  9.  
  10.         Frame.__init__(self, master)
  11.         self.grid()
  12.         self.lienzo = Canvas(width=560, height=560)
  13.         self.draw()
  14.  
  15.     def draw(self):
  16.  
  17.         #Pasto
  18.         self.lienzo.create_rectangle(0, 0,560, 560, fill ='darkGreen')
  19.         self.lienzo.grid(row=0, column=0, rowspan=30, columnspan=10)
  20.  
  21.         #Calles
  22.         self.lienzo.create_rectangle(200, 0, 360, 560, fill ='darkGray', outline ='darkGray') #Vertical
  23.         self.lienzo.create_line(280, 0, 280, 560, fill ='lightGray') #Línea Vertical
  24.         self.lienzo.create_rectangle(0, 200, 560, 360, fill ='darkGray', outline='darkGray') #Horizontal
  25.         self.lienzo.create_line(0,280, 560,280, fill ='lightGray') #Línea Horizontal
  26.         self.lienzo.create_rectangle(200,200, 360,360, fill ='darkGray', outline='yellow', outlinestipple='gray75', width=4) #Centro
  27.  
  28.         #Labels
  29.         self.lienzo.create_text(220, 15, text="Alpha")
  30.         self.lienzo.create_text(320, 545, text="Gamma")
  31.         self.lienzo.create_text(15, 340, text="Beta")
  32.         self.lienzo.create_text(545, 220, text="Delta")
  33.  
  34.         #LightsAlpha
  35.         self.lienzo.create_oval(210, 180, 220, 190, fill='cyan') #AR
  36.         self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
  37.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  38.  
  39.         #LightsGamma
  40.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  41.         self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
  42.         self.lienzo.create_oval(340, 370, 350, 380, fill='cyan') #CR
  43.  
  44.         #LightsBeta
  45.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  46.         self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
  47.         self.lienzo.create_oval(180, 340, 190, 350, fill='cyan') #BR
  48.  
  49.         #LightsDelta
  50.         self.lienzo.create_oval(370, 210, 380, 220, fill='cyan') #DR
  51.         self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
  52.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  53.  
  54.         #Pantalla
  55.         self.screen = Text(height=30, width=25, wrap=WORD, bd=15)
  56.         self.screen.grid(row=0, column=11, rowspan=10)
  57.  
  58.         #Botones
  59.         self.repeatButton = Button(text="Go!", command = self.run)
  60.         self.repeatButton.grid(row=11, column=10, columnspan=40, rowspan=40)
  61.         self.update()
  62.  
  63.     def run(self):
  64.  
  65.         morning_ratio = morning.evaluate(app.hora)
  66.         noon_ratio = noon.evaluate(app.hora)
  67.         night_ratio = night.evaluate(app. hora)
  68.  
  69.         if morning_ratio > noon_ratio:
  70.             eval_ratio = morning_ratio
  71.         elif noon_ratio > night_ratio:
  72.             eval_ratio = noon_ratio
  73.         else:
  74.             eval_ratio = night_ratio
  75.  
  76.         N = 4 + 4 * eval_ratio
  77.         n = 2
  78.  
  79.         self.screen.delete(0.0,END)
  80.  
  81.         #Tiempo 1 -- AS+AR y CS+CR
  82.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  83.         self.lienzo.create_oval(224, 180, 234, 190, fill='green') #AS
  84.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  85.         self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
  86.         self.lienzo.create_oval(326, 370, 336, 380, fill='green') #CS
  87.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  88.         self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
  89.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  90.         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")
  91.  
  92.         #Pausa
  93.         self.update()
  94.         time.sleep(N)
  95.  
  96.         #Tiempo 2 -- BS+BR y DS+DR
  97.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  98.         self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
  99.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  100.         self.lienzo.create_oval(180, 326, 190, 336, fill='green') #BS
  101.         self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
  102.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  103.         self.lienzo.create_oval(370, 224, 380, 234, fill='green') #DS
  104.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  105.         self.screen.insert(END, "Beta y Delta: Puedes ir al frente y doblar a la derecha por " + str("%.2f" % N) + " segundos\n")
  106.  
  107.         #Pausa
  108.         self.update()
  109.         time.sleep(N)
  110.  
  111.         #Tiempo 3 -- AL+AS+AR
  112.         self.lienzo.create_oval(238, 180, 248, 190, fill='green') #AL
  113.         self.lienzo.create_oval(224, 180, 234, 190, fill='green') #AS
  114.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  115.         self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
  116.         self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
  117.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  118.         self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
  119.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  120.         self.screen.insert(END, "Alpha: Puedes ir al frente y doblar a la derecha e izquierda  por " + str(n) + " segundos\n")
  121.  
  122.         self.update()
  123.         time.sleep(n)
  124.  
  125.         #Tiempo 4 -- CL+CS+CR
  126.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  127.         self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
  128.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  129.         self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
  130.         self.lienzo.create_oval(326, 370, 336, 380, fill='green') #CS
  131.         self.lienzo.create_oval(312, 370, 322, 380, fill='green') #CL
  132.         self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
  133.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  134.         self.screen.insert(END, "Gamma: Puedes ir al frente y doblar a la derecha e izquierda  por " + str(n) + " segundos\n")
  135.  
  136.         self.update()
  137.         time.sleep(n)
  138.  
  139.         #Tiempo 5 -- BL+BS+BR
  140.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  141.         self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
  142.         self.lienzo.create_oval(180, 312, 190, 322, fill='green') #BL
  143.         self.lienzo.create_oval(180, 326, 190, 336, fill='green') #BS
  144.         self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
  145.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  146.         self.lienzo.create_oval(370, 224, 380, 234, fill='red') #DS
  147.         self.lienzo.create_oval(370, 238, 380, 248, fill='red') #DL
  148.         self.screen.insert(END, "Beta: Puedes ir al frente y doblar a la derecha e izquierda  por " + str(n) + " segundos\n")
  149.  
  150.         self.update()
  151.         time.sleep(n)
  152.  
  153.      #Tiempo 6 -- DL+DS+DR
  154.         self.lienzo.create_oval(238, 180, 248, 190, fill='red') #AL
  155.         self.lienzo.create_oval(224, 180, 234, 190, fill='red') #AS
  156.         self.lienzo.create_oval(180, 312, 190, 322, fill='red') #BL
  157.         self.lienzo.create_oval(180, 326, 190, 336, fill='red') #BS
  158.         self.lienzo.create_oval(326, 370, 336, 380, fill='red') #CS
  159.         self.lienzo.create_oval(312, 370, 322, 380, fill='red') #CL
  160.         self.lienzo.create_oval(370, 224, 380, 234, fill='green') #DS
  161.         self.lienzo.create_oval(370, 238, 380, 248, fill='green') #DL
  162.         self.screen.insert(END, "Delta: Puedes ir al frente y doblar a la derecha e izquierda  por " + str(n) + " segundos\n")
  163.  
  164.         self.update()
  165.         time.sleep(n)
  166.  
  167.         self.hora += 0.5
  168.  
  169. class mf(object):
  170.  
  171.     def __init__(self, p):
  172.         self._p = p[:]
  173.  
  174. class trapecio(mf):
  175.  
  176.     def evaluate(self,x):
  177.         if x<=self._p[0]:
  178.             return 0
  179.         elif self._p[0] < x <= self._p[1]:
  180.             return (x - self._p[0])/(self._p[1] - self._p[0])
  181.         elif self._p[1] < x <= self._p[2]:
  182.             return 1
  183.         elif self._p[2] < x <= self._p[3]:
  184.             return 1 - (x - self._p[2])/(self._p[3] - self._p[2])
  185.         else:
  186.             return 0
  187.  
  188. def convertToTime(tiempo):
  189.     hr = int(math.floor(tiempo))
  190.     mins = int(math.trunc((tiempo - hr) *60))
  191.  
  192.     if mins < 10:
  193.         return "%d:0%d" % (hr, mins)
  194.     else:
  195.         return "%d:%d" % (hr, mins)
  196.    
  197.  
  198.  
  199. mainwin = Tk()
  200. mainwin.title("Semáforo v5.2")
  201. mainwin.geometry("800x560")
  202. app = Application()
  203.  
  204. app.update()
  205.  
  206. morning = trapecio([5, 7, 9, 11])
  207. noon = trapecio([11, 14, 16, 17])
  208. night = trapecio([17, 19, 21,23])
  209.  
  210. mainwin.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement