gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:38:04 28 4
gpt4 key购买 nike

我正在以编程方式将 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/

28 4 0