Gonzo
Gonzo

Reputation: 2133

Tkinter: Determine Widget Position relative to Root Window

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.

I tried: can.grid_bbox --> (0,0,0,0) ? can.grid_info can.grid_location cget("offset") pointerx

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

---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()

Upvotes: 3

Views: 7633

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385890

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

Upvotes: 3

Related Questions