How to Create a Traffic Light Animation in Python?

  • Thread starter Sthiel
  • Start date
  • Tags
    Light
In summary: TrafficLights().start() main()In summary, the conversation is about creating a traffic light in python using a rectangle and circles on a canvas. The lights change from green to yellow, yellow to red, and red to green, with only one color being on at a time. The
  • #1
Sthiel
6
0
Trying to create a traffic light in python, simply by creating a rectangle and circles in a canvas. The lights change from green to yellow, yellow to red, and red to green. Only one color can be on at a time. Start the animation with the green light on. The program waits for two seconds before changing from one color to another.from Tkinter import *class TrafficLights(Frame):

def __init__(self):
"""Sets up the window and widgets.""" Frame.__init__(self)
self.master.title("Traffic Lights")
self.grid()
x='white'
y='white'
z='green'
# create a canvas and place in this frame
self.canvas = Canvas(self, width = 300, height = 400,
bg = "white")
self.canvas.grid(row = 0, column = 0)
self.canvas.create_rectangle(100, 50, 200, 350)

self.canvas.create_oval(100, 50, 200, 150, fill=x)
self.canvas.create_oval(100, 150, 200, 250, fill=y)
self.canvas.create_oval(100, 250, 200, 350, fill=z)

def main():
TrafficLights().mainloop()

main()That is what I have so far and am not too sure on the best way to go from here. I'm pretty sure i need to use after() and update() but I just need assistance with setting up the animation to fill the circles
 
Technology news on Phys.org
  • #2
and change from one color to another. import timefrom Tkinter import *class TrafficLights(Frame): def __init__(self): """Sets up the window and widgets.""" Frame.__init__(self) self.master.title("Traffic Lights") self.grid() x='white' y='white' z='green' # create a canvas and place in this frame self.canvas = Canvas(self, width = 300, height = 400, bg = "white") self.canvas.grid(row = 0, column = 0) self.canvas.create_rectangle(100, 50, 200, 350) self.light1 = self.canvas.create_oval(100, 50, 200, 150, fill=x) self.light2 = self.canvas.create_oval(100, 150, 200, 250, fill=y) self.light3 = self.canvas.create_oval(100, 250, 200, 350, fill=z) def start(self): self.update_lights() def update_lights(self): self.canvas.itemconfigure(self.light1, fill='red') self.canvas.itemconfigure(self.light2, fill='white') self.canvas.itemconfigure(self.light3, fill='white') self.after(2000, self.update_lights2) def update_lights2(self): self.canvas.itemconfigure(self.light1, fill='white') self.canvas.itemconfigure(self.light2, fill='yellow') self.canvas.itemconfigure(self.light3, fill='white') self.after(2000, self.update_lights3) def update_lights3(self): self.canvas.itemconfigure(self.light1, fill='white') self.canvas.itemconfigure(self.light2, fill='
 

1. What is a Python-Traffic Light?

A Python-Traffic Light is a traffic light system that uses the programming language Python to control its functions and display signals for traffic control.

2. How does a Python-Traffic Light work?

A Python-Traffic Light works by using code written in the Python programming language to control the timing and sequence of the lights. It receives input from sensors and switches to determine when to change the signals.

3. What are the benefits of using Python for a traffic light system?

Using Python for a traffic light system allows for more flexibility and customization in programming compared to traditional traffic lights. It also allows for easier integration with other systems and technologies.

4. Is Python the only programming language used for traffic lights?

No, there are other programming languages used for traffic lights such as C and Java. However, Python is becoming increasingly popular due to its simplicity and versatility.

5. Can Python-Traffic Lights be controlled remotely?

Yes, depending on the setup and programming, Python-Traffic Lights can be controlled remotely through the use of sensors, switches, and a network connection.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Sci-Fi Writing and World Building
Replies
2
Views
2K
Back
Top