4
我試圖讓用戶在單擊鼠標右鍵時刪除行。我已經綁定按鈕3新聞發佈會在畫布上,並通過了以下內容功能刪除Python tkinter畫布上的行
def eraseItem(self,event):
objectToBeDeleted = self.workspace.find_closest(event.x, event.y, halo = 5)
if objectToBeDeleted in self.dictID:
del self.dictID[objectToBeDeleted]
self.workspace.delete(objectToBeDeleted)
但是沒有任何反應,當我右鍵單擊線。我已經單獨測試了字典,並且行對象被正確存儲。
這裏是我的綁定:
self.workspace.bind("<Button-3>", self.eraseItem)
每請求其他一些片段從字典初始化
def __init__(self, parent):
self.dictID = {}
... Some irrelevant code omitted
對於線創建我有兩個處理器,一個上點擊和上釋放其中提請座標之間的線
def onLineClick(self, event):
self.coords = (event.x, event.y)
def onLineRelease(self, event):
currentLine = self.workspace.create_line(self.coords[0], self.coords[1], event.x, event.y, width = 2, capstyle = ROUND)
self.dictID[currentLine] = self.workspace.coords(currentLine)
print(self.dictID.keys()) #For testing dictionary population
print(self.dictID.values()) #For testing dictionary population
字典打印這裏罰款。請注意,這些是一個類中的所有功能。
你的代碼(至少這個函數)看起來很好。事件處理程序是否正確綁定?嘗試打印'objectToBeDeleted'。 – 2013-03-15 15:41:53
是的,它正確地打印適當的畫布ID後,我打印變量賦值 – user2148990 2013-03-15 15:46:36
然後,我只能建議你是刪除條件之前的畫布項。但絕對這個功能沒有任何問題。 – 2013-03-15 15:49:57