turtle.ondrag() function in Python (original) (raw)

Last Updated : 25 Aug, 2025

turtle.ondrag() function is used to bind a function to the mouse-drag event on a turtle object. This means when you click and drag the mouse over the turtle on the canvas, the specified function will be called with the current mouse coordinates.

**Syntax:

turtle.ondrag(fun, btn=1, add=None)

**Parameter:

**Returns: This function does not return anything.

**Example :

python `

import turtle

def fxn(x, y): turtle.ondrag(None) turtle.setheading(turtle.towards(x, y)) turtle.goto(x, y)
turtle.ondrag(fxn)

turtle.speed(10)
sc = turtle.Screen()
sc.setup(400, 300)

turtle.ondrag(fxn)
sc.mainloop()

`

**Output :

**Explanation: