I'm trying to simulate an American traffic light, with 3 circles on a rectangle, all drawn on a set Canvas. The simulation is supposed to mirror "animation" by changing which light is displayed every 2 seconds in the following order: green > yellow > red > green, etc forever.
The only way I can think of to do this is by using a canvas.move(), canvas.after(), canvas.update() pattern to move a filled oval object to superimpose one unfilled circle at a time. I've gotten the logic down to move a circle at the proper speed and in the correct order. The thing is, I just instantiate a circle filled with "green", but I can't change it to be "yellow" or "red" using this method. It seems silly to have to canvas.delete("filled") and redraw it in a new place with a different fill every 2 seconds, because that's a lot to do for such a simple program.
Question 1: Is there a way I can just alter the fill
option for my filled Canvas object at will, using some method or other means?
Question 2: Am I approaching this scenario incorrectly? Is there a better way to simulate this?
Yes you should be able to change settings of the canvas with config
.
Likewise, use itemconfig
to change items on the canvas. This does require that you save a handle to the item or tag them.
Example from tkinterbook:
i = w.create_line(xy, fill="red")
w.coords(i, new_xy) # change coordinates
w.itemconfig(i, fill="blue") # change color