如何在 tkinter 中获取相对于父窗口小部件的鼠标位置?

发布于 2024-09-10 13:39:18 字数 32 浏览 3 评论 0原文

我需要获取相对于 tkinter 窗口的鼠标位置。

I need to get the mouse position relative to the tkinter window.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-09-17 13:39:18

一般来说,您永远不需要“获取”此信息,因为它是作为传入的事件对象的一部分提供给您的。您可能只在响应事件时需要此信息,并且事件会为您提供此信息。

更简洁地说,要获取信息,您只需从事件对象中检索它即可。

这是一个例子:

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

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()
記柔刀 2024-09-17 13:39:18

获取鼠标移动事件的屏幕坐标(x/y_root)并减去窗口的屏幕坐标(window.winfo_rootx()/ y())。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文