对 Tkinter bind_class 感到困惑 [英] Confused about Tkinter bind_class
问题描述
我定义了 GCanvas,它是 Canvas 的扩展.我的意图是在类级别绑定到 GCanvas.它不起作用.
I define GCanvas, an extension of Canvas. My intention is to bind to GCanvas at the class level. It isn't working.
我也尝试绑定到 tk.Canvas 但它也不起作用.绑定到 root
或 GCanvas 实例工作正常.(这两种选择都对我没有用,但我只是尝试了看看发生了什么.).运行 OS X,El Capitan.
I also tried to bind to tk.Canvas and it doesn't work either. Binding to root
or to the GCanvas instance works fine. (Neither of this alternatives is useful to me, but I just tried them to see what happened.). Running 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 类名.更准确地说,在这种情况下,它指的是 bind 标签,它恰好与 tk 类同名,也恰好与核心 Tkinter 类之一同名(例如:Toplevel
、Canvas
等).
The "class" in bind_class
refers to the internal class name used by the tk library, not the python class name. More precisely, in this context it refers to a bind tag, which happens to be the same name as the tk class, which also happens to be the same name as one of the core Tkinter classes (eg: Toplevel
, Canvas
, etc).
要在类级别绑定到 GCanvas
,最简单的方法是将名为 GCanvas
的绑定标记添加到您的画布,如下例所示:>
To bind to GCanvas
at the class level, the simplest thing would be to add a bind tag named GCanvas
to your canvas, as in the following example:
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
:
You can then use bind_class
like so:
root.bind_class("GCanvas", "<Enter>", GCanvas.enter)
root.bind_class("GCanvas", "<Leave>", GCanvas.leave)
有关绑定标签的更多信息,请参阅其他一些 tkinter 问题的答案:
For more information about bind tags, see these answers to some other tkinter questions:
这篇关于对 Tkinter bind_class 感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!