from tkinter import *
def onObjectClick(event, obj):
canv.itemconfig(obj, width=2)
def no_onObjectClick(event, obj):
canv.itemconfig(obj, width=1)
root = Tk()
canv = Canvas(root, width=500, height=500)
can_obj = []
w=10
ii=0
while ii < 2:
points = [w,100, w+10,0, w+20,100]
ln = canv.create_line(points, fill='green')
can_obj.append(ln)
w+=10
ii+=1
ii=0
##this part is working fine
##canv.tag_bind(can_obj[1], '<Enter>', lambda event : onObjectClick(event, can_obj[1]))
##canv.tag_bind(can_obj[1], '<Leave>', lambda event : no_onObjectClick(event, can_obj[1]))
##canv.tag_bind(can_obj[0], '<Enter>', lambda event : onObjectClick(event, can_obj[0]))
##canv.tag_bind(can_obj[0], '<Leave>', lambda event : no_onObjectClick(event, can_obj[0]))
#this is not working as above
for obj in can_obj:
canv.tag_bind(obj, '<Enter>', lambda event : onObjectClick(event, obj))
canv.tag_bind(obj, '<Leave>', lambda event : no_onObjectClick(event, obj))
canv.pack()
#root.mainloop()
在Windows上使用python 3.4,它在使用循环时仅突出显示最后一个对象。但手动(没有循环其正常工作,如评论部分中所用)...任何解决方案??