【问题标题】:tkinter: moving an image by clickingtkinter:通过单击移动图像
【发布时间】:2021-01-15 20:09:37
【问题描述】:

我正在尝试创建一个功能,允许您通过单击在画布上选择图像,然后使用相同的鼠标按钮单击画布以将此图像传送到第二次单击的坐标。

目前我知道如何使用find_closest 选择图像,使用coords 移动它,但我不知道如何编写代码。

现在我有:

self.cnv.bind('<Button-1>', self.select)
def select(self, event):
    self.item = self.cnv.find_closest(event.x, event.y)
    self.cnv.coords(self.item, event.x, event.y)

但是当我点击它时它只是直接移动图像,而我想先选择它然后再点击一次。

【问题讨论】:

  • 尝试向我们展示您目前拥有的代码?
  • 试过move()方法?
  • 你想要的是非常接近拖放小部件,所以我建议你看一下Drag and Drop widgets tkinter以获得一些想法。
  • @martineau:是的,尽管您链接到了一篇关于移动小部件的文章,而不是画布项目。解决方案不会完全相同。

标签: python image tkinter


【解决方案1】:

我可能会这样做的方法是,当您检测到点击时,首先检查是否有任何项目具有“已选择”标签。如果有一个或多个带有“选定”标签的项目,请移动它们。如果没有“已选择”标签,则将标签添加到最近的项目。

逻辑看起来像这样:

def handle_click(event):
    canvas = event.widget
    items = canvas.find_withtag("selected")
    if items:
        # move the items to the new coordinates
        ...
   else:
        # add the 'select' tag to the item closest
        # to the click 
        ...

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多