hi guys.. im having problems with my traffic lights. i want to execute the procesing of the traffic lights infinitely (i.e red to green to red continuously). im providing my code hoping to get a positive response soon. thanks..
import Tkinter as tk
import time
import sys
import datetime as dt
from Tkinter import *
class ElapsedTimeClock(Label):
def __init__(self, parent, *args, **kwargs):
Label.__init__(self, parent, *args, **kwargs)
self.lastTime = ""
t = time.localtime()
self.zeroTime = dt.timedelta(hours=t[3], minutes=t[4], seconds=t[5])
self.tick()
def tick(self):
# get the current local time from the PC
now = dt.datetime(1, 1, 1).now()
elapsedTime = now - self.zeroTime
time2 = elapsedTime.strftime('%H:%M:%S')
# if time string has changed, update it
if time2 != self.lastTime:
self.lastTime = time2
self.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
self.after(200, self.tick)
class TrafficLight(tk.Frame):
"""inherits tk.Frame"""
def __init__(self, parent=None):
tk.Frame.__init__(self, parent, bg='green')
#self.grid()
# create a canvas to draw on
#self.cv = tk.Canvas(self, width=260, height=280, bg='white')
#self.cv.grid()
self.make_widgets()
#self.red_light()
def make_widgets(self):
canvas.create_rectangle(168, 90, 190, 140)
def red_light(self):
# imagine a square box with
# upper left corner coordinates x1, y1
# lower right corner coordinates x2, y2
# and sides of length span for each circle
span = 12
x1 = 173
y1 = 95
x2 = x1 + span
y2 = y1 + span
canvas.create_oval(x1, y1, x2, y2, fill='red')
#canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
#canvas.create_oval(x1, y1+38, x2, y2+38, fill='green')
canvas.create_oval(x1, y1+28, x2, y2+28, fill='white')
def green_light(self):
# imagine a square box with
# upper left corner coordinates x1, y1
# lower right corner coordinates x2, y2
# and sides of length span for each circle
span = 12
x1 = 173
y1 = 95
x2 = x1 + span
y2 = y1 + span
#canvas.create_oval(x1, y1, x2, y2, fill='red')
#canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
canvas.create_oval(x1, y1+28, x2, y2+28, fill='green')
canvas.create_oval(x1, y1, x2, y2, fill='white')
root=tk.Tk()
root.title("Traffic Simulation")
canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF")
clock = ElapsedTimeClock(root, font=('times', 12, 'bold'), bg='white')
clock.pack(fill=BOTH, expand=1)
canvas.pack()
light = TrafficLight()
y=light.red_light()
z=light.green_light
m = light.red_light
root.after(5000,z)
root.after(10000,m)
#root.after(10000,m)
#canvas.update()
#start = time.time()
root.mainloop()
light.mainloop()