python - 如何通过事件修改 Tkinter Canvas 中的标签

标签 python canvas tkinter event-handling

我正在以编程方式将 tag_binds 添加到 Canvas 上所有具有标签“tag”的对象:

self.canvas.tag_bind("tag","<Button 2>",self.tag_highlight)

self.canvas 包含用户添加的所有对象(都用“tag”标记)。

但是,在 tag_highlight 中,我只想处理 Canvas 中的实际元素。 event.widget 似乎没有帮助,它只是一个 4 个 float 的元组,我无法链接到任何 Canvas 项目。我尝试使用以下方法,但无济于事:

self.canvas.find_closest(event.x,event.y)

我想做的是有一个像这样的句柄:

t=self.canvas.getitem(event.widget)

这样我就可以在以下方面使用它:

self.canvas.Move(t,30,20)
print self.canvas.coords(t)

等等。 也许我只是错过了显而易见的东西?

最佳答案

您可以使用标签“current”,它指的是“current”对象。当前对象在 official tk documentation 中是这样描述的:

The tag current is managed automatically by Tk; it applies to the current item, which is the topmost item whose drawn area covers the position of the mouse cursor (different item types interpret this in varying ways; see the individual item type documentation for details). If the mouse is not in the canvas widget or is not over an item, then no item has the current tag.

关于python - 如何通过事件修改 Tkinter Canvas 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493569/

相关文章:

python - 如何在html中显示python应用程序?

python - 返回一个新列表的函数,其中包含原始列表中的值,这些值可被函数参数中的给定数字整除

python - 根据 map 中键的存在有选择地检索

javascript - 用鼠标单击对象使 Canvas 对象消失

javascript - 如何延迟加载 Canvas 元素

Python Tkinter - 选项数据库仅部分工作

python - 如何清除 Tkinter ListBox Python

python - Scikit-learn 逻辑回归的性能比用 Python 自行编写的逻辑回归要差

python - 修改给定 "path"的嵌套字典中的元素

javascript - html2canvas:图像在 Firefox 上看起来模糊