-1

Basically I want to access the coordinates of 2 mouse clicks, and then I even have to perform operations using those coordinates .

I have found this code but this is running infinitely and not terminating after 2 times as how I wanted

import turtle

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

turtle.onscreenclick(get_mouse_click_coor)

turtle.mainloop()
Dash
  • 1,191
  • 7
  • 19

1 Answers1

2

How about the follwing approach. Click on the window twice and then do_whatever() will be called with the two coordinates:

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81