tkinter canvas not updating color

python, tkinter

Solution

The problem you are having is that you are creating the canvas on every iteration, and packing it below all of the other canvases. When you say the object color isn't changing, that's because you are observing the first canvas you created; the color is changing for the most recently created canvas but it is off screen.

Change your code to create a single canvas and your code will otherwise work just fine. For example:

import Tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.frame_Light = tk.Frame(self, background="bisque")
        self.frame_Light.pack(side="top", fill="both", expand=True)
        self.light_on = True
        self.canvas = tk.Canvas(self.frame_Light)
        self.canvas.create_oval(10, 10, 30, 30, fill="yellow", tags="light")
        self.canvas.pack(side="top", fill="both", expand=True)
        self.draw_light()

    def draw_light(self):

        if self.light_on:
            self.canvas.itemconfig("light", fill="blue")
            self.light_on = False
            print "on"
        else:
            self.canvas.itemconfig("light", fill="red")
            self.light_on = True
            print "of"

        self.after(1000, self.draw_light)

app = App()
app.mainloop()

Problem

I draw an oval into a canvas which works perfect also it show color red and the loops runs fine too cause I can see the print. Its supposed to change the color ever 1000ms. But its not changing the color? ``` def draw_light(self): w = tk.Canvas(self.frame_Light) w.pack() w.create_oval(10, 10, 30, 30, fill="yellow", tags="light") if self.light_on: w.itemconfig("light", fill="blue") self.light_on = False print "on" else: w.itemconfig("light", fill="red") self.light_on = True print "of" self.app.after(1000, self.draw_light) ``` UPDATE changed the code to your suggestions still generates only the red canvas thats it ``` def draw_light(self): self.ligth_canvas = tk.Canvas(self.frame_Light) self.ligth_canvas.pack() self.ligth_canvas.create_oval(10, 10, 30, 30, fill="yellow", tags="light") self.app.after(0, self.change_light) def change_light(self): i = self.ligth_canvas.find_withtag("light") if self.light_on: self.ligth_canvas.itemconfig(i, fill="blue") self.light_on = False print "on" else: self.ligth_canvas.itemconfig(i, fill="red") self.light_on = True print "of" self.app.after(5000, self.change_light) ```

Original source