Hello everyone,
I've been learning python since the past few days.
I have tried several ways but nothing seems to work!!
All I want to do is, randomly generate some moving ovals after some regular or irregular interval of time. I have tried canvas.after but it didn’t worked.
The ovals should stop when the lights are red.
I would be really grateful if anyone could help me out. Thanking you in advance.
Here is the code:

import Tkinter as tk
import time
import sys
import datetime as dt 
from Tkinter import *
import random
       

class TrafficLight(tk.Frame):
    """inherits tk.Frame"""
    def __init__(self, parent=None):
        tk.Frame.__init__(self, parent, bg='green')  
        
        
        

    def make_widgets(self,x1,y1,x2,y2):
        canvas.create_rectangle(x1,y1,x2,y2)
        canvas.create_oval(x1+5, y1+5, x2-5, y2-22, fill='red')
        canvas.create_oval(x1+5, y1+22, x2-5, y2-5, fill='white')

    def red_light(self):
        canvas.create_oval(173,106,185,118, fill='red')
        canvas.create_oval(173,123,185,135, fill='white')
        #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
        #canvas.create_oval(x1, y1, x2, y2, fill='green')
        self.after(5000,self.green_light)
        self.green_light_on = False

    def green_light(self):
        
        #canvas.create_oval(x1, y1, x2, y2, fill='red')
        #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
        canvas.create_oval(173,123,185,135, fill='green')
        canvas.create_oval(173,106,185,118, fill='white')
        self.after(5000,self.red_light)
        self.green_light_on = True



    def red_light1(self):
        canvas.create_oval(347,106,359,118, fill='red')
        canvas.create_oval(347,123,359,135, fill='white')
        #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
        #canvas.create_oval(x1, y1, x2, y2, fill='green')
        self.after(5000,self.green_light1)
        self.green_light_on1 = False

    def green_light1(self):
        
        #canvas.create_oval(x1, y1, x2, y2, fill='red')
        #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
        canvas.create_oval(347,123,359,135, fill='green')
        canvas.create_oval(347,106,359,118, fill='white')
        self.after(5000,self.red_light1)
        self.green_light_on1 = True


    


class Car(object):
  def __init__(self, a,b,c,d, outline='blue', fill='blue'):
    self.rect = canvas.create_oval(a,b,c,d, outline=outline, fill=fill)
    self.speed = (0, 0)

  def set_speed(self, x, y):
    self.speed = x, y
    
  def move(self):
    canvas.move(self.rect, self.speed[0], self.speed[1])
    self.light = TrafficLight()
    red1=canvas.coords(canvas.create_oval(173,106,185,118))      
    red2=canvas.coords(canvas.create_oval(347,106,359,118))    
    vehicle=canvas.coords(self.rect)
    
                
    if vehicle < red1:
        red11=canvas.coords(canvas.create_oval(168,81,180,93, fill= "white",outline= "red"))  
               
        if light.green_light_on:                
            self.speed=3,0                    
            

        elif not light.green_light_on:                   
            self.speed=0,0                               
            if vehicle<red11:                
                self.speed=3,0


    if  (vehicle > red1 and red2>vehicle):
        red22=canvas.coords(canvas.create_oval(342,81,354,93, fill= "white",outline= "red"))
        if light.green_light_on1:                
            self.speed=3,0                     
            

        elif not light.green_light_on1:                   
            self.speed=0,0            
                           
            if vehicle<red22:                
                self.speed=3,0               








root=tk.Tk()
root.title("Traffic Simulation")
canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF")
canvas.pack()




 
# 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)



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)


# 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")

light = TrafficLight()


light.make_widgets(168, 101, 190, 140)
light.make_widgets(342, 101, 364, 140)

y=light.red_light() 
y1=light.red_light1()

  
car1 = Car(20, 155, 40, 170, outline='blue', fill='blue')
car2 = Car(20, 175, 40, 190, outline='red', fill = 'red')
car3 = Car(40, 180, 60, 195, outline='Yellow', fill = 'Green')

# move car
while True:   
    
    car1.move()    
    canvas.after(10000,car2.move)
    car3.move()
    
    canvas.update()

root.mainloop()

Recommended Answers

All 3 Replies

Your cars are initialized with a speed of 0, 0. You should call their set_speed method to define another value. If you don't start the cars, they won't move !

i tried but this didn't solved my problem!!!!
i would be thankful if anyone could post a simple example where a new oval is continuously created after a random period of time( e.g A new moving oval is created after every 10 -15 seconds), and is destroyed when it reaches a particular X or Y coordinate.
Looking forward for a reply..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.