如何在 tkinter 窗口中设置鼠标位置 [英] How can I set the mouse position in a tkinter window

查看:72
本文介绍了如何在 tkinter 窗口中设置鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 3d 渲染程序,它根据鼠标在屏幕上的位置围绕观察者旋转世界.世界旋转的弧度量由这条线定义

I have a 3d rendering program that rotates the world around an observer based on the position the mouse is on the screen. The amount in radians that the world is rotated is defined by this line

    glob.worldx=-(w.winfo_pointerxy()[0]-xy[0])/250

其中xy[0]为屏幕中心的x坐标

Where xy[0] is the x coordinate of the center of the screen

这意味着观察者视野可以旋转的量受鼠标可以移动的距离的限制.如果我能让鼠标回到屏幕中央,我就可以解决这个问题.有什么想法吗?

This means that the amount that the observers field of view can be rotated is limited by the distance the mouse can go. If I could get the mouse to come back to the center of the screen I could solve this problem. Any ideas?

推荐答案

好消息是有办法做到这一点.

The good news is that there is a way to do it.

中间新闻是它没有很好的记录.

The intermediate news is that it's not well documented.

坏消息是它仅适用于某些平台.

The bad news is that it only works on some platforms.

另一个中间消息是,您至少可以在某些平台上脱离 Tk.

The other intermediate news is that you can step outside of Tk on at least some platforms.

在 Tcl/Tk 中执行此操作的方法是使用 -warp 1 生成 事件.这方面的文档很少,分散在几个不同的页面上(从 开始)bind),但在此处描述了详细信息.基本上就是这样:

The way to do this in Tcl/Tk is by generating a <Motion> event with -warp 1. The documentation on this is sparse, and scattered around a few different pages (start at bind), but the details are described here. Basically, it's just this:

event generate . <Motion> -warp 1 -x 50 -y 50

那么,您如何从 Tkinter 中做到这一点?

So, how do you do this from Tkinter?

好吧,event_generate 没有记录在任何地方, 事件或 warp 参数也没有记录......但它是如果您知道 Tk 是如何映射到 Tkinter 的,那么很简单:

Well, event_generate isn't documented anywhere, and neither is the <Motion> event, or the warp parameter… but it's pretty simple to figure out if you know how Tk maps to Tkinter:

window.event_generate('<Motion>', warp=True, x=50, y=50)

这确实会生成一个事件,正如您通过绑定 所看到的.这是一个简单的测试程序:

And this does indeed generate an event, as you can see by binding <Motion>. Here's a simple test program:

from tkinter import *

root = Tk()

def key(event):
    root.event_generate('<Motion>', warp=True, x=50, y=50)

def motion(event):
    print('motion {}, {}'.format(event.x, event.y))

root.bind('<Key>', key)
root.bind('<Motion>', motion)
root.mainloop()

运行它,单击窗口以确保它有焦点,四处移动光标,您将看到它打印出如下内容:

Run it, click the window to make sure it has focus, move the cursor around, and you'll see it print out something like this:

motion 65, 69
motion 65, 70
motion 65, 71

然后按一个键,它会打印出这个:

Then hit a key, and it'll print out this:

motion 50, 50

这很好......除了它实际上可能无法移动您的光标,在这种情况下,所有这些都是欺骗 Tk 认为光标移动了.

Which is great… except that it may not actually be able to move your cursor, in which case all this does is trick Tk into thinking the cursor moved.

浏览各种论坛,看起来是这样的:

From skimming various forums, it looks like:

  • Mac:不起作用.
    • 您必须拥有 Tk 8.4.something 或更高版本.我找不到这方面的错误,但是您可以依靠任何官方 Windows 二进制安装的 Python 2.7 或 3.x+ 来使用 8.4.
    • 您也不得运行全屏应用(使用 Tk,您通常不会运行).
    • 在 Vista 及更高版本上,在某些情况下它不起作用.这可能与不拥有桌面会话或不是本地控制台会话有关,或者可能与需要管理员或其他权限有关.
    • 如果它不起作用,直接转到 Win32 API 很容易.
    • 您的窗口管理器不得禁止其他客户端扭曲指针.幸运的是,这似乎并不常见.
    • 如果您遇到这个问题,那就没有办法了.

    对于 Mac,您想要生成并发送 NSMouseMoved 事件.最简单的方法是使用 pyobjc(如果你使用的是 Apple 的 Python,它是内置的;否则你必须安装它):

    For Mac, you want to generate and send an NSMouseMoved event. The easy way to do this is with pyobjc (which is built in if you're using Apple's Python; otherwise you have to install it):

    app = Foundation.NSApplication.sharedApplication()
    event = Foundation.NSEvent.mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure_(
        Foundation.NSMouseMoved, (50, 50), 0, 0,
        app.mainWindow().windowNumber(), None, 0, 0, 0.0)
    app.sendEvent_(event)
    

    对于 Windows,您想要调用 SetCursorPos API,或生成并发送 MOUSEEVENT.前者不适用于例如 DirectX 游戏;后者可能不适用于远程桌面.对于这种情况,您可能想要前者.无论哪种方式,最简单的方法是安装 pywin32,然后就是:

    For Windows, you want to call the SetCursorPos API, or generate and send a MOUSEEVENT. The former will not work with, e.g., DirectX games; the latter may not work with remote desktops. For this case, you probably want the former. Either way, the easiest way to do this is to install pywin32, and then it's just:

    win32api.SetCursorPos((50, 50))
    

    这篇关于如何在 tkinter 窗口中设置鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆