在tkinter(Python)中获取在画布上绘制的项目的填充色或任何其他属性

rah*_*agi 2 python canvas tkinter

我想获得在tkinter的画布中绘制的项目的填充颜色或其他任何属性。

  def createWidgets(self):
    self.canvas_1= tk.Canvas(self, bg='#FAFAFA',selectforeground='#BBDEFB');
    i=self.canvas_1.create_rectangle((self.canvas_1.winfo_reqwidth()/2)+100,
                                   (self.canvas_1.winfo_reqheight()/2)+50,
                                   (self.canvas_1.winfo_reqwidth()/2)+150,
                                   (self.canvas_1.winfo_reqheight()/2)+100, 
                                    fill='#FF4081',width=0)
    self.canvas_1.grid();
    color=               #want to access the fill color of item i using some getter functions.
Run Code Online (Sandbox Code Playgroud)

Jam*_*ent 5

您可以使用以下itemcget方法执行此操作:
effbot

因此您可以使用:

color = self.canvas_1.itemcget(i, "fill")
Run Code Online (Sandbox Code Playgroud)