Python 海龟中获取鼠标点击坐标

Eyl*_*lon 7 python turtle-graphics python-turtle

我试图获取海龟屏幕中鼠标单击的坐标,但我的代码无法正常工作。我猜这与时间有关,但我尝试添加 5 秒的延迟,但没有帮助。

这是我的代码:

def get_mouse_click_coor(x,y):
   print [x,y]
turtle.onscreenclick(get_mouse_click_coor)
Run Code Online (Sandbox Code Playgroud)

请帮我理解代码有什么问题,谢谢:)

cdl*_*ane 11

您的代码看起来基本正确,但让我们使其完整:

import turtle

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

turtle.onscreenclick(get_mouse_click_coor)

turtle.mainloop()
Run Code Online (Sandbox Code Playgroud)

上面的工作原理——窗口上的所有点击都会将 x 和 y 坐标打印到控制台。尝试一下,如果它不适合您,请告诉我。

我只需要获取一次坐标

这很简单,我们只需在第一次单击时关闭单击处理程序即可:

import turtle

def get_mouse_click_coor(x, y):
    turtle.onscreenclick(None)
    print(x, y)

turtle.onscreenclick(get_mouse_click_coor)

turtle.mainloop()
Run Code Online (Sandbox Code Playgroud)