hi guys am stuck with my project.. i want to make the car stop when the lights are red and then move when its green. im providing you my code hoping you can help me.. 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, 80, 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 = 85
        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')


    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 = 85
        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')




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
root.after(10000,z)




 
# make roads
l1=canvas.create_line(140,150,200,150,
                      200,150,200,50, width=3)
l2=canvas.create_line(250,150,250,50,
                      250,150,374,150,
                      374,150,374,50, width=3)
l3=canvas.create_line(424,150,424,50,
                      424,150,542,150,
                      542,150,542,50, width=3)
l4=canvas.create_line(592,150,592,50,
                      592,150,674,150,
                      674,150,674,50, width=3)
l5=canvas.create_line(724,150,724,50,
                      724,150,850,150, width=3)

l6=canvas.create_line(140,200,200,200,
                      200,200,200,300, width=3)
l7=canvas.create_line(250,200,250,300,
                      250,200,344,200,
                      344,200,344,300, width=3)
l8=canvas.create_line(394,200,394,300,
                      394,200,542,200,
                      542,200,542,300, width=3)
l9=canvas.create_line(592,200,592,300,
                      592,200,674,200,
                      674,200,674,300, width=3)
l10=canvas.create_line(724,200,724,300,
                       724,200,850,200, width=3)

# color roads
rect=canvas.create_rectangle(140,152,850,198, fill="#999999", outline="#999999")
rect1=canvas.create_rectangle(202,50,248,300, fill="#999999", outline="#999999")
rect2=canvas.create_rectangle(376,50,422,152, fill="#999999", outline="#999999")
rect3=canvas.create_rectangle(346,198,392,300, fill="#999999", outline="#999999")
rect4=canvas.create_rectangle(544,52,590,300, fill="#999999", outline="#999999")
rect5=canvas.create_rectangle(676,52,722,300, fill="#999999", outline="#999999")


# lines on road
l11=canvas.create_line(140,175,850,175, width=2, fill="White")
l12=canvas.create_line(225,50,225,150,248,150, width=2, fill="White")
l13=canvas.create_line(202,200,248,200, width=2, fill="White")
l14=canvas.create_line(225,200,225,300, width=2, fill="White")
l15=canvas.create_line(399,50,399,150,422,150, width=2, fill="White")
l16=canvas.create_line(346,200,392,200, width=2, fill="White")
l17=canvas.create_line(369,200,369,300, width=2, fill="White")
l18=canvas.create_line(567,50,567,150,590,150, width=2, fill="White")
l19=canvas.create_line(699,50,699,150,722,150, width=2, fill="White")
l20=canvas.create_line(544,200,590,200, width=2, fill="White")
l21=canvas.create_line(567,200,567,300, width=2, fill="White")
l22=canvas.create_line(676,200,722,200, width=2, fill="White")
l23=canvas.create_line(699,200,699,300, width=2, fill="White")
l24=canvas.create_line(200,152,200,175, width=2, fill="White")
l25=canvas.create_line(374,152,374,175, width=2, fill="White")
l26=canvas.create_line(542,152,542,175, width=2, fill="White")
l27=canvas.create_line(674,152,674,175, width=2, fill="White")
l28=canvas.create_line(724,198,724,175, width=2, fill="White")
l29=canvas.create_line(592,198,592,175, width=2, fill="White")
l30=canvas.create_line(394,198,394,175, width=2, fill="White")
l31=canvas.create_line(250,198,250,175, width=2, fill="White")



# create car
#car = canvas.create_rectangle(20, 155, 40, 170, outline='blue', fill='blue')
#car=canvas.create_oval(22,158,30,166, outline='blue', fill='blue')
#car1=canvas.create_oval(922,178,930,186, outline='blue', fill='blue')
#car2=canvas.create_oval(722,188,730,196, outline='yellow', fill='yellow')

car1 = canvas.create_rectangle(20, 155, 40, 170, outline='blue', fill='blue')
# move car


for x in range(200):
    y= x = 5
    
    time.sleep(0.025)
    canvas.move(car1,x,0)
    canvas.update()
    #canvas.event_generate(car)



#start = time.time()
root.mainloop()
light.mainloop()

You can use a variable named something like self.red_light_on. Set it to True when the light changes to red, and False on a change to green or yellow. Then

if not self.red_light_on:
     canvas.move(car1,x,0)
     canvas.update()

i've tried several pieces of code but still it doesn't work. can u please fix it for me? thanks.

can anyone plz fix this problem because am having a similar problem with my codes. looking forward for a reply. thanks in advance..

This article has been dead for over six months. Start a new discussion instead.