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

2024-10-01 11:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我已经找到了这段代码,但它是无限运行的,并没有按照我想要的方式在两次之后终止

import turtle

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

turtle.onscreenclick(get_mouse_click_coor)

turtle.mainloop()

Tags: 代码importgetdef方式鼠标clickprint
1条回答
网友
1楼 · 发布于 2024-10-01 11:31:21

下面的方法怎么样。单击窗口两次,然后使用两个坐标调用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()

相关问题 更多 >