微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决单击对象后,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 举报,一经查实,本站将立刻删除。