Tkinter 中 Canvas 的 tag_bind 方法

tag_bind method of Canvas in Tkinter

我想使用 create_image 方法将多个处理程序绑定到在 canvas 上绘制的图像上的事件。 但是只有一个处理程序在工作..第一个。

systemcanvas.tag_bind(image_id,'<ButtonRelease-1>',fun1)
systemcanvas.tag_bind(image_id, "<ButtonRelease-1>", fun2)

如何让这两个函数都绑定到图片上?

将事件绑定到小部件或 canvas 项目后,命令将一直绑定,直到您取消绑定或覆盖它。您只需将新命令绑定到该项目或小部件即可覆盖它,以避免您可以添加可选参数 add='+'.

import tkinter as tk

def test1(event):
    print('test 1 is running')
def test2(event):
    print('test 2 is running')

root = tk.Tk()
cnvs = tk.Canvas(root)
itm  = cnvs.create_text(0,0, anchor='nw',text='default')
cnvs.tag_bind(itm,'<Button-1>', test1)
cnvs.tag_bind(itm,'<Button-1>', test2,add='+')
cnvs.pack()
root.mainloop()

If script is prefixed with a “+”, then it is appended to any existing binding for sequence; otherwise script replaces any existing binding. If script is an empty string then the current binding for sequence is destroyed, leaving sequence unbound. In all of the cases where a script argument is provided, bind returns an empty string.

Source