繁体   English   中英

如何在Python中获取当前鼠标position的坐标?

[英]How to get coordinates of current mouse position in Python?

我想使用 Python Turtle 制作一个程序,在单击时将海龟移动到鼠标坐标。 但我不知道如何编写返回鼠标 position 的 function。 或者,如果更容易理解,有 HTML 和 JavaScript 代码转换为 Python 代码:

function getcoords(event)
{
  console.log(event.clientX + " " + event.clientY);
}

addEventListener("click", getcoords)

根据this answer,您可以使用以下代码:

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

Python 逻辑与您的 Javascript 逻辑几乎相同:

from turtle import Screen

def getcoords(x, y):
    print(x, y)

screen = Screen()

screen.onclick(getcoords)

screen.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM