uccjshrimpton

Traffic Light GUI

Jul 11th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #Imports the required libraries
  2. from time import sleep
  3. from tkinter import *
  4.  
  5. #This procedure manages which light order is currently being displayed
  6. def loop_lights():
  7.     red=["red","white","white"]
  8.     red_yellow=["red","yellow","white"]
  9.     yellow=["white","yellow","white"]
  10.     green=["white","white","green"]
  11.     off=["white","white","white"]
  12.  
  13.     arrangement_order=[red,red_yellow,yellow,green,off]
  14.  
  15.     current_light_order = 0
  16.     while True:
  17.        
  18.         if current_light_order > 4:
  19.             current_light_order = 0
  20.  
  21.         else:
  22.             draw_lights(arrangement_order[current_light_order])
  23.             current_light_order += 1
  24.             sleep(2)
  25.  
  26. #This procedure actually draws the lights when asked to by the previous procedure
  27. def draw_lights(light_order):
  28.     canvas.delete("all")
  29.     canvas.create_oval(5,5,145,145,fill=light_order[0])
  30.     canvas.create_oval(5,150,145,295,fill=light_order[1])
  31.     canvas.create_oval(5,300,145,445,fill=light_order[2])
  32.     canvas.update()
  33.            
  34.  
  35. #This code manages the creation of the GUI
  36. main_window = Tk()
  37.  
  38. button_start = Button(main_window, text="Start!", command=loop_lights)
  39. canvas = Canvas(main_window, width=150, height=450)
  40.  
  41. button_start.pack()
  42. canvas.pack()
  43.  
  44. main_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment