Python 海龟-如何获得鼠标光标在窗口中的位置?

Python 海龟-如何获得鼠标光标在窗口中的位置?,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我将如何在海龟屏幕中查找鼠标指针的当前位置?我想要它,这样我就可以在单击和移动光标之前找到鼠标位置。我搜索过谷歌,除了点击后如何获得职位,这个网站找不到任何东西。返回Tkinter画布 ,您可以通过winfo_pointerx和获取当前鼠标指针坐标。winfo_pointery: canvas = turtle.getcanvas() x, y = canvas.winfo_pointerx(), canvas.winfo_pointery() # or # x, y = canvas.winf

我将如何在海龟屏幕中查找鼠标指针的当前位置?我想要它,这样我就可以在单击和移动光标之前找到鼠标位置。我搜索过谷歌,除了点击后如何获得职位,这个网站找不到任何东西。

返回Tkinter画布

,您可以通过
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)
def运动(事件):
x、 y=事件.x,事件.y
打印(“{},{}.”格式(x,y))
canvas=turtle.getcanvas()
画布绑定(“”,运动)
请注意,事件仅在鼠标指针悬停在海龟画布上时触发


所有这些坐标都是窗口坐标(原点
(0,0)
位于turtle窗口的左上角),而不是turtle坐标(原点
(0,0)
位于画布中心),因此如果要将它们用于turtle定位或定向,您必须进行一些转换。

您正在使用Python的模块吗?如果是这样,您有没有看过文档中的内容?有。它只有onClick、click、drag和on-click-release的事件。我需要它来监听鼠标指针的位置。我只需要在海龟屏幕上获取鼠标坐标。所以我可以让乌龟旋转,看看老鼠。