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