我定义了GCanvas,它是Canvas的扩展。我的意图是在 class 级别绑定到GCanvas。它不起作用。
我也试图绑定到tk.Canvas,它也不起作用。绑定到root
或GCanvas实例可以正常工作。 (这两种选择对我都没有用,但是我只是试着看看它们发生了什么。)运行OS X,El Capitan。
import Tkinter as tk
class GCanvas(tk.Canvas, object):
def __init__(self, master, **kwargs):
tk.Canvas.__init__(self, master, kwargs)
@staticmethod
def enter(e):
print "enter", e.widget, e.x, e.y
@staticmethod
def leave(e):
print "leave", e.widget
@staticmethod
def motion(e):
print "motion", e.widget, e.x, e.y
approach = "bindinstance"
root = tk.Tk()
gc = GCanvas(root, width=400, height=300)
print "root is", root, "gc is", gc
gc.pack()
if approach == "bindGCanvas":
print "binding to GCanvas"
root.bind_class(GCanvas, '<Enter>', GCanvas.enter)
root.bind_class(GCanvas, '<Leave>', GCanvas.leave)
#root.bind_class(GCanvas, '<Motion>', GCanvas.motion)
elif approach == "bindCanvas":
print "binding to Canvas"
root.bind_class(tk.Canvas, '<Enter>', GCanvas.enter)
root.bind_class(tk.Canvas, '<Leave>', GCanvas.leave)
#root.bind_class(tk.Canvas, '<Motion>', GCanvas.motion)
elif approach == "bindinstance":
print "binding to instance"
gc.bind('<Enter>', GCanvas.enter)
gc.bind('<Leave>', GCanvas.leave)
#gc.bind('<Motion>', GCanvas.motion)
else:
print "binding to root"
root.bind('<Enter>', GCanvas.enter)
root.bind('<Leave>', GCanvas.leave)
#root.bind('<Motion>', GCanvas.motion)
root.mainloop()
最佳答案
bind_class
中的“类”是指tk库使用的内部类名,而不是python类名。更准确地说,在这种情况下,它是指绑定标签,该标签恰好与tk类的名称相同,也恰好与核心Tkinter类之一的名称相同(例如:Toplevel
,Canvas
等)。
要在类级别绑定到GCanvas
,最简单的方法是在画布上添加一个名为GCanvas
的绑定标签,如以下示例所示:
class GCanvas(tk.Canvas, object):
def __init__(self, master, **kwargs):
...
# get the current bind tags
bindtags = list(self.bindtags())
# add our custom bind tag before the Canvas bind tag
index = bindtags.index("Canvas")
bindtags.insert(index, "GCanvas")
# save the bind tags back to the widget
self.bindtags(tuple(bindtags))
然后,您可以像这样使用
bind_class
:root.bind_class("GCanvas", "<Enter>", GCanvas.enter)
root.bind_class("GCanvas", "<Leave>", GCanvas.leave)
有关绑定标签的更多信息,请参见其他一些tkinter问题的答案:
关于python-2.7 - 对Tkinter bind_class感到困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40421993/