解除tkinter中所有画布项的绑定
我刚接触tkinter这个库...
我在tkinter中无法解除画布上物体的绑定。
这些物体是通过下面的代码绑定的:
self.canvas = Canvas(root, background="white")
self.canvas.create_line(x1, y1, x2, y2, fill="blue", tags=scale_index_tag)
self.canvas.tag_bind(scale_index_tag, "<ButtonPress-1>", self.Add_weight)
我尝试过:
self.canvas.unbind("<ButtonPress-1>")
self.canvas.unbind_all(self.Add_weight)
self.canvas.unbind_all("<ButtonPress-1>")
...但是没有成功。
我甚至试着删除所有画布上的物体,然后重新生成它们并不绑定,但当我点击鼠标时,Add_weight方法还是会被调用...
self.canvas.delete(ALL)
1 个回答
2
如果你打算用 tag_bind
来给按钮绑定一些功能,那你也需要用 tag_unbind
来解除这个绑定。还有,unbind
(就像 bind
一样)需要两个参数,这里指的是标签和按钮。
self.canvas.tag_unbind(scale_index_tag, `"<ButtonPress-1>"`)