Python Tkinter画布拖离中心

Python Tkinter画布拖离中心,python,tkinter,scrollbar,draggable,tkinter-canvas,Python,Tkinter,Scrollbar,Draggable,Tkinter Canvas,有没有办法找出Tkinter中一个滚动条在画布上滚动了多少像素?我正在写一个应用程序,它可以拖放行。但是,每当我尝试拖动一行时,在滚动一定量之后,该行在滚动之前会自动以该行末尾所在的位置为中心。我的想法是将坐标偏移相同的量,以便在拖动时,线正好位于鼠标图标下方 #output is the canvas #wires is a list containing the item ID's of all the lines in my project def moveLine(e): out

有没有办法找出Tkinter中一个滚动条在画布上滚动了多少像素?我正在写一个应用程序,它可以拖放行。但是,每当我尝试拖动一行时,在滚动一定量之后,该行在滚动之前会自动以该行末尾所在的位置为中心。我的想法是将坐标偏移相同的量,以便在拖动时,线正好位于鼠标图标下方

#output is the canvas
#wires is a list containing the item ID's of all the lines in my project
def moveLine(e):
    outputItem = output.find_withtag('current')
    try:
        x0, y0, x1, y1 = output.coords(outputItem)
    except:
        pass

    if len(output.coords(outputItem)) != 0 and outputItem[0] in wires:
        output.coords(outputItem, x0, y0, e.x, e.y)
        output.itemconfig(outputItem, fill='blue')

有两种方法可以解决这个问题。你可以使用tkinter小部件滚动条,但移动图标将被延迟。第二种方法是制作你们自己的滚动条(点击事件,滑块的运动事件,别忘了阻止Y轴),并在这个运动事件上拖动图标和滑块。它会更平滑。如果您愿意,我可以向您发送自定义滚动条的代码。

这有助于回答您的问题吗?将
output.coords(outputItem,x0,y0,e.x,e.y)
更改为
output.coords(outputItem,x0,y0,output.canvasx(e.x),output.canvasy(e.y))
。这两种方法都不是解决此问题的好方法。另外,这个答案没有足够的细节让某人真正实施这些建议。