ここでの問題は、特定のタイプの最も近いオブジェクトを見つけることができないということです。コードを提供して、私の意味を理解してもらうのが最善です。
from Tkinter import *
root = Tk()
f=Frame(root)
f.grid()
w=Canvas(f)
def identify(event): ## this should identify the tag near to click
item = w.find_closest(event.x, event.y)[0]
print item
line1=w.create_line(50,50,150,150, width=5, tags="line")
line2=w.create_line(100,100,100,350, width=3, tags="line")
line3=w.create_line(150,150,150,450, width=3, tags = "line")
w.grid(row=0, column=0)
w.bind("<ButtonRelease-1>", identify)
u=Frame(f)
u.grid(row=0, column=1)
root.mainloop()
ここでわかるように、画面上の任意の場所をクリックすると、最も近いオブジェクトが返されます。これは私が達成したいことですが、私が無視したい他のオブジェクトを画面に表示しています。タグを使用してこれを行うことができますが、ここでの問題は、何らかの理由で実際のオブジェクトをクリックする必要があることです。このコードは、私の問題を示すために使用されています。私の実際のコードはハノイの塔のゲームで、ディスクがスナップできるように最も近い極を見つけることを目指していますが、ディスクを移動する前にすべての極をクリックしないと、最も近い極を見つけることができません。
これが私の問題を示すコードです。注: "w.bind("", identify)" を "w.tag_bind("line", "", identify)" に変更しただけです。
from Tkinter import *
root = Tk()
f=Frame(root)
f.grid()
w=Canvas(f)
def identify(event): ## this should identify the tag near to click
item = w.find_closest(event.x, event.y)[0]
print item
line1=w.create_line(50,50,150,150, width=5, tags="line")
line2=w.create_line(100,100,100,350, width=3, tags="line")
line3=w.create_line(150,150,150,450, width=3, tags = "line")
w.grid(row=0, column=0)
w.tag_bind("line", "<ButtonRelease-1>", identify)
u=Frame(f)
u.grid(row=0, column=1)
root.mainloop()