如何在python的海龟屏幕上获取鼠标单击的坐标?

时间:2020-07-02 16:24:01

标签: python turtle-graphics python-turtle

基本上,我想访问两次鼠标单击的坐标,然后甚至必须使用这些坐标执行操作。

我已经找到了这段代码,但是它可以无限运行,并且不会按照我想要的方式终止两次

import turtle

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

turtle.onscreenclick(get_mouse_click_coor)

turtle.mainloop()

1 个答案:

答案 0 :(得分:2)

下面的方法怎么样。单击窗口两次,然后将使用两个坐标调用do_whatever()

from turtle import Screen, Turtle
from functools import partial

def do_whatever(start, end):
    ''' Replace the body of this function. '''

    turtle.penup()
    turtle.goto(start)
    turtle.pendown()
    turtle.goto(end)

def get_mouse_click_1(x, y):
    screen.onclick(partial(get_mouse_click_2, (x, y)))

def get_mouse_click_2(position, x, y):
    screen.onclick(None)
    do_whatever(position, (x, y))

screen = Screen()

turtle = Turtle()
turtle.hideturtle()

screen.onclick(get_mouse_click_1)

screen.mainloop()