0

I want to make a program using Python Turtle, that moves the turtle to mouse coordinates while clicking. But I don't know how to write a function that returns mouse position. Or, if it will be more understandable, there is HTML and JavaScript code to convert into Python code:

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

addEventListener("click", getcoords)
cdlane
  • 40,441
  • 5
  • 32
  • 81

2 Answers2

0

According to this answer, you can use the code below:

canvas = turtle.getcanvas()
x, y = canvas.winfo_pointerx(), canvas.winfo_pointery()
aga
  • 21
  • 4
0

The Python logic is pretty much the same as your Javascript logic:

from turtle import Screen

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

screen = Screen()

screen.onclick(getcoords)

screen.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81