python - How can I bind multiple canvas elements together in order to change their color simultaneously? -


so, these lines have created/drawn:

from tkinter import *   root = tk()  f= frame(root) f.pack() c = canvas(f,bg = "black") c.pack() line1 = c.create_line(10,0,10,50,fill = "white",activefill = "blue",tag = "one") line_side1 = c.create_line(0,25,10,25,fill= "white", activefill = "blue",tag = "one") line2 = c.create_line(30,0,30,50,fill = "white",activefill = "blue",tag = "one") line_side2 = c.create_line(30,25,40,25,fill= "white", activefill = "blue",tag = "one") c.pack()  root.mainloop() 

so, want lines should color blue when hover mouse on them.

i've tried using tag_bind option, helpful if show me how it.

although answer @aleksandermonk works fine, think binding tag "one" easier in case, when you're planning on making more lines. can use tag instead of id in both tag_bind , itemconfigure function:

from tkinter import *  def change_color(event):     if event.type == "7":   # enter         event.widget.itemconfigure("one", fill="blue")     elif event.type == "8": # leave         event.widget.itemconfigure("one", fill="white")  root = tk() f = frame(root) c = canvas(f, bg="black") f.pack()     c.pack()  line1      = c.create_line(10, 0,10,50, fill="white", tag="one") line_side1 = c.create_line( 0,25,10,25, fill="white", tag="one") line2      = c.create_line(30, 0,30,50, fill="white", tag="one") line_side2 = c.create_line(30,25,40,25, fill="white", tag="one")  c.tag_bind("one", "<enter>", change_color) c.tag_bind("one", "<leave>", change_color)  root.mainloop() 

Comments