我有一个3d渲染程序,可以根据鼠标在屏幕上的位置围绕观察者旋转世界。世界旋转的弧度数由此线定义
glob.worldx=-(w.winfo_pointerxy()[0]-xy[0])/250
其中xy [0]是屏幕中心的x坐标
这意味着观察者视野可以旋转的数量受到鼠标距离的限制。如果我能让鼠标回到屏幕中心,我可以解决这个问题。有什么想法吗?
答案 0 :(得分:9)
好消息是有办法实现。
中间新闻是没有详细记录。
坏消息是它只适用于某些平台。
另一个中间新闻是你可以在至少某些平台上走出Tk。
在Tcl / Tk中执行此操作的方法是使用<Motion>
生成-warp 1
事件。关于此的文档很稀疏,分散在几个不同的页面上(从bind
开始),但详细信息描述为here。基本上就是这样:
event generate . <Motion> -warp 1 -x 50 -y 50
那么,你是如何从Tkinter做到的?
好吧,event_generate
没有在任何地方记录,<Motion>
事件或warp
参数也没有记录......但是如果你知道Tk如何映射到Tkinter的:
window.event_generate('<Motion>', warp=True, x=50, y=50)
这确实会生成一个事件,正如您可以通过绑定<Motion>
看到的那样。这是一个简单的测试程序:
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()
运行它,点击窗口确保它有焦点,移动光标,你会看到它打印出这样的东西:
motion 65, 69
motion 65, 70
motion 65, 71
然后点击一个键,它会打印出来:
motion 50, 50
哪个好......除了它实际上可能无法移动光标,在这种情况下,所有这一切都会让Tk认为光标移动了。
从浏览各种论坛,它看起来像:
对于Mac,您希望生成并发送NSMouseMoved
事件。执行此操作的简单方法是使用pyobjc
(如果您使用Apple的Python,则内置;否则您必须安装它):
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
,然后就是:
win32api.SetCursorPos((50, 50))
答案 1 :(得分:0)
对于任何有兴趣将光标移动到屏幕上的绝对位置的人(使用@abarnert 的 tkinter 方法):
# Moves the mouse to an absolute location on the screen
def move_mouse_to(x, y):
# Create a new temporary root
temp_root = tk.Tk()
# Move it to +0+0 and remove the title bar
temp_root.overrideredirect(True)
# Make sure the window appears on the screen and handles the `overrideredirect`
temp_root.update()
# Generate the event as @abarnert did
temp_root.event_generate("<Motion>", warp=True, x=x, y=y)
# Make sure that tcl handles the event
temp_root.update()
# Destroy the root
temp_root.destroy()
此功能不应干扰其他 tkinter 窗口。请注意,此函数会创建一个刷新两次的新窗口,因此它可能会很慢并且用户可能会注意到创建的窗口。