2013-03-15 177 views
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 

字典打印這裏罰款。請注意,這些是一個類中的所有功能。

+0

你的代碼(至少這個函數)看起來很好。事件處理程序是否正確綁定?嘗試打印'objectToBeDeleted'。 – 2013-03-15 15:41:53

+0

是的,它正確地打印適當的畫布ID後,我打印變量賦值 – user2148990 2013-03-15 15:46:36

+0

然後,我只能建議你是刪除條件之前的畫布項。但絕對這個功能沒有任何問題。 – 2013-03-15 15:49:57

回答

0

我試着根據你的代碼做一個工作示例,現在我知道問題在哪裏:find_closest返回一個元素,如果找到了一個元素,所以當你檢查它是否在字典中時,首先你必須檢索元組的第一個元素。

def eraseItem(self,event): 
    tuple_objects = self.workspace.find_closest(event.x, event.y, halo = 5) 
    if len(tuple_objects) > 0 and tuple_objects[0] in self.dictID: 
     objectToBeDeleted = tuple_objects[0] 
     del self.dictID[objectToBeDeleted] 
     self.workspace.delete(objectToBeDeleted)