Sunday, August 9, 2020

Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates



Printing every time you move





If you want to print every time you move, you should define your own function which will do that as well as tell the turtle to go somewhere. I think this will work because turtle is a singleton.
def gotoandprint(x, y):
    gotoresult = turtle.goto(x, y)
    print(turtle.xcor(), turtle.ycor())
    return gotoresult

screen.onscreenclick(gotoandprint)



import turtle
from turtle import Screen, Turtle

screen = Screen()
t = Turtle("turtle")
t.speed(-1)

def dragging(x, y):  # These parameters will be the mouse position
    t.ondrag(None)
    t.setheading(t.towards(x, y))
    t.goto(x, y)
    t.ondrag(dragging)

def clickRight():
    t.clear()

def main():  # This will run the program
    turtle.listen()
    
    t.ondrag(dragging)  # When we drag the turtle object call dragging
    turtle.onscreenclick(clickRight, 3)

    screen.mainloop()  # This will continue running main() 

main()

No comments:

Post a Comment

Introduction to Keras and TensorFlow for Training Deep Learning Classifiers

 ### Introduction to Keras and TensorFlow for Training Deep Learning Classifiers **Keras and TensorFlow** are powerful tools in the realm of...