2016-03-01 310 views
0

我该如何去查找乌龟屏幕中鼠标指针的当前位置?我需要它,所以我点击鼠标并在移动光标时点击鼠标。我已经搜索谷歌,除了点击后如何获得这个位置,这个网站还找不到任何东西。Python(海龟模块) - 鼠标光标在窗口中的位置

+0

你使用Python的['turtle'](https://docs.python.org/library/turtle.html)模块吗?如果是这样,你有没有看过文档中的“使用事件”(https://docs.python.org/2/library/turtle.html#using-events)? –

+0

是的。它只包含onClick事件,点击拖放,点击发布。我需要它来监听鼠标指针的位置。 – helpneeded92

+0

在乌龟屏幕上获取鼠标坐标是我所要求的。所以我可以让乌龟旋转并看看鼠标。 – helpneeded92

回答

1

turtle.getcanvas()返回Tkinter画布。

Like with a Tkinter window,你可以得到它目前的鼠标指针的坐标由winfo_pointerx.winfo_pointery

canvas = turtle.getcanvas() 
x, y = canvas.winfo_pointerx(), canvas.winfo_pointery() 
# or 
# x, y = canvas.winfo_pointerxy() 

如果你想只应对移动速度而不是如轮询鼠标指针在循环中的位置,注册一个事件:

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

canvas = turtle.getcanvas() 
canvas.bind('<Motion>', motion) 

请注意,事件只在鼠标指针悬停在乌龟画布上时触发。

所有这些坐标将是窗口坐标(在乌龟窗口的左上角有原点(0, 0)),而不是乌龟坐标(原点(0, 0)在画布中心),所以如果你想使用它们进行乌龟定位或方向,你必须做一些转变。