鼠标位置在TKINTER中

发布于 2025-01-22 10:04:19 字数 463 浏览 3 评论 0原文

我正在尝试将鼠标位置相对于TKINTER窗口。问题是当我用标签或按钮的鼠标内的鼠标与该标签或按钮相对的位置时。我希望仅针对我的窗口而不是其他小部件将坐标划分。 这是我遇到问题的代码的一部分。

绑定按钮:

select.bind('<Motion>', motion)
select.bind('<Any-Button>', on_click)

函数以检测鼠标位置,然后单击鼠标:

def on_click(event):
    if event.num == 1:
        print(x_mouse, y_mouse)   
def motion(event):
    global x_mouse, y_mouse
    x_mouse, y_mouse = event.x, event.y

I am trying to get the mouse position relative to a tkinter window. The problem is when I am with the mouse inside of a label or a button, because it gives me the position relative to that label or button. I want the coordinates to be ralated only for my window not the other widgets.
Here is a piece of my code where I have the problem.

Bind the button:

select.bind('<Motion>', motion)
select.bind('<Any-Button>', on_click)

Functions to detect mouse position and when it is clicked:

def on_click(event):
    if event.num == 1:
        print(x_mouse, y_mouse)   
def motion(event):
    global x_mouse, y_mouse
    x_mouse, y_mouse = event.x, event.y

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文