在 Tcl Tk 中将鼠标按钮单击绑定到画布上的任意位置
我希望我的画布在单击画布区域上的任意位置时单击鼠标按钮时执行操作。我已经能够绑定单击,但仅当它在现有对象上进行时:
$this/zinc bind all <Button-1> [list select_shape $this]
我尝试使用相同的命令而不指定标签:
$this/zinc bind <Button-1> [list select_shape $this]
它给出了错误。
通过给出一个空字符串而不是一个标签:
$this/zinc bind "" <Button-1> [list select_shape $this]
它就像我写了all
一样。
我应该提供什么标签
,以便它可以在画布中的任何位置工作,或者如何避免指定标签
?
I want my canvas to preform an action when a mouse button is clicked anywhere on the canvas area. I've been able to bind a click, but only when it's made on an existing object:
$this/zinc bind all <Button-1> [list select_shape $this]
I tried using the same command without specifying a tag:
$this/zinc bind <Button-1> [list select_shape $this]
It gave an error.
And by giving an empty string instead of a tag:
$this/zinc bind "" <Button-1> [list select_shape $this]
it acted as if I wrote all
.
What tag
do I give so that it'll work anywhere in the canvas, or how do I avoid specing a tag
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
画布的
bind
方法仅允许您绑定到项目(以及应用于项目的标签),并且仅适用于所有 Tk 事件的子集。如果您想要整个画布的事件 - 或者检测其他类型的事件 - 请使用全局bind
命令:或者,放置一个完全透明的矩形(
-fill
和-outline
设置为空字符串)位于所有其他项目下方,并将其用作最后的表面。就这样,如果没有其他东西能发出咔嗒声,那就会了。或者,您甚至可以将这样的透明项目放在所有内容之上(在这种情况下,它将拦截所有鼠标事件)并这样做。请注意,矩形(以及多边形,当您需要非矩形热区域时)在这种方式上很特殊:大多数项目在您看不到的部分中没有响应,但完全透明的矩形在其整个区域上都有响应。这有各种有趣的用途。(有时您可以将底层矩形与另一个项目(例如背景图像项目)组合起来。)
The canvas's
bind
method only lets you bind to items (and tags applied to items) and then only for a subset of all Tk's events. If you want an event for the whole canvas — or to detect other types of events — use a globalbind
command:Alternatively, put a fully transparent rectangle (both
-fill
and-outline
set to the empty string) underneath all the other items and use that as a surface-of-last-resort. Like that, if nothing else picks up the click, that will. Or you could even put such a transparent item on top of everything (in which case it will intercept all mouse events) and do it that way. Note that rectangles (and polygons, for when you want non-rectangular hot areas) are special this way: most items are unresponsive in the parts of them you can't see, but fully-transparent rectangles are responsive over their whole area. This has all sorts of interesting uses.(Sometimes you can combine the underlying rectangle with another item, e.g., a background image item.)