turtle.onrelease() function in Python

The turtle.onrelease() function binds a function to a mouse-button-release event on the turtle. The function will be called when the mouse button is released.

Syntax: turtle.onrelease(fun, btn=1, add=None)
Parameters: fun – a function with two arguments (x, y coordinates); btn – mouse button number.

Let us see an example to implement the turtle.onrelease() function in Python:

Demo34.py

# turtle.onrelease() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

def on_click(x, y):
    t.pencolor("red")
    t.goto(x, y)

def on_release(x, y):
    t.pencolor("black")

t.onclick(on_click)        # Bind mouse click
t.onrelease(on_release)    # Bind mouse release

window.mainloop()

The following is the output:

turtle.onrelease() function in Python

In the above code, we followed the below steps:

Absolutely, Amit — here’s the explanation in your preferred bullet-point format:

  • import turtle – Loads Python’s turtle graphics module.
  • window = turtle.Screen() – Creates the drawing window.
  • t = turtle.Turtle() – Initializes the turtle object.
  • def on_click(x, y): – Defines a function to run when the turtle is clicked.
    • t.pencolor(“red”) – Changes the pen color to red.
    • t.goto(x, y) – Moves the turtle to the clicked coordinates.
  • def on_release(x, y): – Defines a function to run when the mouse button is released.
    • t.pencolor(“black”) – Resets the pen color to black.
  • t.onclick(on_click) – Binds the click event to the turtle; triggers on_click when the turtle is clicked.
  • t.onrelease(on_release) – Binds the mouse release event to the turtle; triggers on_release when the mouse button is released.
  • window.mainloop() – Starts the event loop to keep the window open and responsive to mouse events.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.onscreenclick() function in Python
turtle.ontimer() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment