這將做到這一點:
from Tkinter import *
master = Tk()
w = Canvas(master, width=200, height=200)
w.pack()
i=w.create_line(0, 0, 100, 100)
# Bind the mouse click to the delete command
w.bind("<Button-1>", lambda e: w.delete(i))
mainloop()
編輯迴應評論:
是,上述方案將在窗口中的任意註冊一個鼠標點擊。如果您希望它只在點擊接近它時刪除該行,則需要更復雜的內容。也就是說,這樣的事情:
from Tkinter import *
master = Tk()
w = Canvas(master, width=200, height=200)
w.pack()
i=w.create_line(0, 0, 100, 100)
def click(event):
# event.x is the x coordinate and event.y is the y coordinate of the mouse
if 80 < event.x < 120 and 80 < event.y < 120:
w.delete(i)
w.bind("<Button-1>", click)
mainloop()
只有當鼠標點擊的x
和y
座標withing線20分這個腳本會刪除該行。
請注意,我不能完全設置此。你將不得不根據你的需要調整它。
我注意到,當鼠標指針離線很遠(一英寸左右)時,這也會刪除線。當指針靠近它時,有沒有辦法刪除這一行? – Platypus
@Platypus - 您可以獲取鼠標單擊的x和y座標,然後僅當它們位於該行的特定距離內時才刪除該行。看我的編輯。 – iCodez
不幸的是,這隻能在靠近行尾的方形區域內工作。 20點區域並不沿線。我認爲必須有一個線性方程式編程入其中。 – Platypus