title: Python/Tkinter - Identify object on click
body: <p>I am trying to create a program that changes the object colour on click from white to black or from white to black depending of the previous colour. I would want the program change the colour only if the object is a rectangle. How can I make the this happen?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import tkinter as tk
  
  root = tk.Tk()
  
  cv = tk.Canvas(root, height=800, width=800)
  cv.pack()
  
  def onclick(event):
      item = cv.find_closest(event.x, event.y)
  
      current_color = cv.itemcget(item, 'fill')
  
      if current_color == 'black':
          cv.itemconfig(item, fill='white')
  
      else:
          cv.itemconfig(item, fill='black')
  
  
  cv.bind('&lt;Button-1&gt;', onclick)
  
  cv.create_line(50, 50, 60, 60, width=2)
  
  cv. create_rectangle(80, 80, 100, 100)
  
  root.mainloop()
  </code></pre>
  
  <p>In this code the program changes the fill colour for any object. I would want it to change it only for rectangles.</p>
  
  <p>Thanks for the help.</p>