开发者

How do I get mouse position relative to the parent widget in tkinter?

开发者 https://www.devze.com 2023-01-07 20:23 出处:网络
I need to get the mouse position relative to the tkinter window开发者_StackOverflow社区. Generally speaking you should never need to \"get\" this information because it is given to you as part of the

I need to get the mouse position relative to the tkinter window开发者_StackOverflow社区.


Generally speaking you should never need to "get" this information because it is given to you as part of the event object that is passed in. You probably only need this information when responding to an event, and the event gives you this information.

Put more succinctly, to get the information you simply have to retrieve it from the event object.

Here's an example:

import Tkinter

class App:
    def __init__(self, root):
        f = Tkinter.Frame(width=100, height=100, background="bisque")
        f.pack(padx=100, pady=100)
        f.bind("<1>", self.OnMouseDown)

    def OnMouseDown(self, event):
        print "frame coordinates: %s/%s" % (event.x, event.y)
        print "root coordinates: %s/%s" % (event.x_root, event.y_root)

root=Tkinter.Tk()
app = App(root)
root.mainloop()


Get the screen coordinates of the mouse move event (x/y_root) and subtract the screen coordinates of the window (window.winfo_rootx()/y()).

0

精彩评论

暂无评论...
验证码 换一张
取 消