如何在tkinter python gui中找到鼠标点击附近的标签

lok*_*esh 2 python user-interface tkinter

如何识别鼠标点击附近的标签.在这里,我的定义"识别"应该识别非常接近鼠标点击的标签.

from Tkinter import *
root = Tk()
f=Frame(root)
f.grid()
w=Canvas(f)
line1=w.create_line(50,50,150,150, width=5, tags="line1")
line2=w.create_line(100,100,100,350, width=3, tags="line2")
line3=w.create_line(150,150,150,450, width=3, tags="lines")
w.grid(row=0, column=0)
w.bind("<Button-1>", identify)
def identify(event): ## this should identify the tag near to click

u=Frame(f)
u.grid(row=0, column=1)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

谢谢

A. *_*das 5

使用find_closest和gettags:

def identify(event):
    item = w.find_closest(event.x, event.y)[0]
    tags = w.gettags(item)
    print tags
Run Code Online (Sandbox Code Playgroud)

顺便说一下,在将函数绑定到事件之前,必须先定义函数.