我正在开发一个拖放功能,它允许我在画布上移动项目。我让它工作(有点)但我只移动了一点,但线在屏幕上射击(最终离开了可见部分画布,所以我无法到达它。我不确定从这里去哪里。下面是我到目前为止创建的拖放代码:
def onPressToMove(self, event): #get initial location of object to be moved
winX = event.x - self.workspace.canvasx(0)
winY = event.y - self.workspace.canvasy(0)
self.dragInfo["Widget"] = self.workspace.find_closest(event.x, event.y, halo = 5)[0]
self.dragInfo["xCoord"] = winX
self.dragInfo["yCoord"] = winY
def onReleaseToMove(self, event): #reset data on release
self.dragInfo["Widget"] = None
self.dragInfo["xCoord"] = 0
self.dragInfo["yCoord"] = 0
def onMovement(self, event):
winX = event.x - self.workspace.canvasx(0)
winY = event.y - self.workspace.canvasy(0)
newX = winX - self.dragInfo["xCoord"]
newY = winY - self.dragInfo["yCoord"]
self.workspace.move(self.dragInfo["Widget"], newX, newY)
dragInfo 是我用来存储数据的字典。最初我认为将画布坐标转换为窗口坐标会有所帮助,但它的作用与没有这些东西相同。