tkinter 画布不更新颜色 [英] tkinter canvas not updating color

查看:27
本文介绍了tkinter 画布不更新颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布上画了一个椭圆,效果很好,它显示红色,循环也很好,因为我可以看到印刷品.它应该每 1000 毫秒改变一次颜色.但它没有改变颜色?

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)

推荐答案

您遇到的问题是您在每次迭代时都创建画布,并将其打包在所有其他画布下方.当您说对象颜色没有改变时,那是因为您正在观察您创建的第一个画布;最近创建的画布的颜色正在改变,但它不在屏幕上.

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

这篇关于tkinter 画布不更新颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆