turtle.getpen() function in Python

The turtle.getpen() function returns a dictionary (a “pen-dict”) with the current pen’s attributes, such as color, width, and drawing state.

Syntax: turtle.getpen()
Parameters: None.

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

Demo64.py

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

import turtle

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

# Get a dictionary of pen properties
pen_properties = t.getpen()
print("Pen properties:", pen_properties)

window.exitonclick()

The following is the output:

turtle.getpen() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Get pen object: pen_properties = t.getpen() returns the pen/turtle object itself (an alias of the turtle), not a dictionary of properties. To get a properties dictionary, use t.pen() instead.
  • Print pen info: print(“Pen properties:”, pen_properties) prints the pen/turtle object representation (or use print(t.pen()) to see a dict of pen settings).
  • Close on click:exitonclick() waits for a mouse click, then closes the window.

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.setundobuffer() function in Python
turtle.getshapes() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment