单击对象后,tkinter 画布不会更新对象的颜色

问题描述

我正在做一个项目,在 python 中使用哪种基本油漆,我被困在这个洪水填充部分,它不会更新对象的颜色,也没有错误,所以我无法弄清楚是哪个部分错了。

from tkinter import *
from tkinter.colorchooser import askcolor

coords = {"x1":0,"y1":0,"x2":0,"y2":0}
lines = []

class Filling():
    def __init__(self,main):
        self.main = main
        self.main.title('Filling')
        self.main.geometry("800x620")
        btncir=Button(main,text="CIRCLE",fg='black',width=8,command=self.circle)
        btncir.place(x=10,y=200)
        btnfill=Button(main,text="FILL",command=self.clickfillrec)
        btnfill.place(x=10,y=250)
        btnclear=Button(main,text="COLOR",command=self.color_choice)
        btnclear.place(x=10,y=300)
        self.canvas = Canvas(self.main,bg='white',bd=5,relief=RIDGE,height=600,width=700)
        self.canvas.place(x=80,y=0)
    def circle(self):
        self.canvas.bind("<ButtonPress-1>",self.circle_click)
        self.canvas.bind("<B1-Motion>",self.drag) 
    def circle_click(self,e):
        coords["x1"] = e.x
        coords["y1"] = e.y
        lines.append(self.canvas.create_oval(coords["x1"],coords["y1"],coords["x1"],coords["y1"]))    
    def drag(self,e):
        coords["x2"] = e.x
        coords["y2"] = e.y
        self.canvas.coords(lines[-1],coords["x2"],coords["y2"])
    def color_choice(self):
        self.DEFAULT_COLOR = self.color
        self.color = askcolor()
    def clickfillrec(self):
        self.canvas.bind("<Button-1>",self.ffillrec)
        self.canvas.bind("<B1-Motion>",self.nothing) 
    def nothing(self,event):
        pass
    def ffillrec(self,event):
        item = self.canvas.find_closest(event.x,event.y)
        x = event.x
        y = event.y
        current_color = self.canvas.itemcget(item,'fill')
        if (x > 0) and (self.canvas.itemcget((event.x-1,event.y),'fill')) == current_color:
            self.canvas.itemconfig((event.x-1,fill = self.color)
        if (y > 0) and (self.canvas.itemcget((event.x,event.y-1),'fill')) == current_color : 
            self.canvas.itemconfig((event.x,fill = self.color)
        if (x < self.canvas.winfo_screenwidth()) and (self.canvas.itemcget((event.x+1,'fill')) == current_color : 
            self.canvas.itemconfig((event.x+1,fill = self.color)
        if (y < self.canvas.winfo_screenheight()) and (self.canvas.itemcget((event.x,event.y+1),fill = self.color)
main = Tk()
p = Filling(main)
main.mainloop()

我尝试了 main.update()self.canvas.update_idletasks() 但仍然不起作用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)