【问题标题】:Tkinter canvas drag off center [duplicate]Tkinter画布拖离中心[重复]
【发布时间】:2020-08-03 20:21:55
【问题描述】:

有没有办法在 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')

【问题讨论】:

  • 这有助于回答您的问题吗? stackoverflow.com/a/7811911/7432
  • output.coords(outputItem, x0, y0, e.x, e.y) 更改为output.coords(outputItem, x0, y0, output.canvasx(e.x), output.canvasy(e.y))

标签: python tkinter scrollbar draggable tkinter-canvas


【解决方案1】:

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

【讨论】:

  • 这些都不是解决这个问题的好方法。另外,这个答案没有足够的细节让某人实际实施这些建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2022-11-02
相关资源
最近更新 更多