本文介绍了Tkinter:确定相对于根窗口的小部件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tkinter MouseWheel 事件只能绑定到根窗口.因此事件位置也是相对于根位置的.对于画布缩放操作,我想获取包含有关画布内光标位置信息的 MouseWheel 事件.为了计算这个,我想我会简单地减去根窗口中画布的位置.现在的问题是,我找不到窗口中的画布位置.

The tkinter MouseWheel event can only be bound to the root window. Thus the event position is also relative to the root position.For a canvas zooming operation, I would like to get the MouseWheel-event with information about the cursor-position within the canvas. To calculate this I thought I would simply subtract the position of the canvas within the root window. Problem now is, that I can not find out the canvas position in the window.

我试过:can.grid_bbox --> (0,0,0,0) ?can.grid_infocan.grid_locationcget(偏移量")指针x

I tried:can.grid_bbox --> (0,0,0,0) ?can.grid_infocan.grid_locationcget("offset")pointerx

还有一些我不记得了.不知怎的,我一直想念它,有人可以给我一个提示吗?

and some others I can't remember. Somehow I keep on missing it could someone give me a hint?

---编辑---要获取 mouseWheel 事件相对于画布的位置,这种方法似乎有效:

---edit---To get the position of the mouseWheel event relative to the canvas, this approach seems to work:

def on_mouse_wheel(self, event):
    xCan = event.x_root - self.can.winfo_rootx()
    yCan = event.y_root - self.can.winfo_rooty()

推荐答案

您想使用 winfo_x 和 winfo_y 方法来获取相对于父级的 x/y 位置.

You want to use the winfo_x and winfo_y methods to get the x/y position relative to the parent.

这篇关于Tkinter:确定相对于根窗口的小部件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:20