我在画布上画了一个椭圆形,效果很好,它也显示为红色,循环也很好,因为我可以看到打印。它应该每 1000 毫秒改变一次颜色。但它不改变颜色?
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 将代码更改为您的建议仍然只生成红色画布
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)