Open In App

turtle.pen() function in Python

Last Updated : 24 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

turtle.pen()

This function is used to return or set the pen’s attributes in a ‘pen-dictionary’ with the following key/value pairs:

  • “shown” : True/False
  • “pendown” : True/False
  • “pencolor” : color-string or color-tuple
  • “fillcolor” : color-string or color-tuple
  • “pensize” : positive number
  • “speed” : number in range 0..10
  • “resizemode” : “auto” or “user” or “noresize”
  • “stretchfactor”: (positive number, positive number)
  • “shearfactor” : number
  • “outline” : positive number
  • “tilt” : number

This dictionary can be used as an argument for a subsequent pen()-call to restore the former pen-state. Moreover, one or more of these attributes can be provided as keyword-arguments. This can be used to set several pen attributes in one statement.

Syntax : turtle.pen(pen=None, **pendict) 

Parameters:

  • pen : a dictionary with some or all of the below listed keys.
  • **pendict : one or more keyword-arguments with the below listed keys as keywords.

Below is the implementation of the above method with some examples :

Example 1 :

Python3




# import package
import turtle
 
# check default values
print(turtle.pen())


Output : 

{‘shown’: True, ‘pendown’: True, ‘pencolor’: ‘black’, ‘fillcolor’: ‘black’, ‘pensize’: 1, ‘speed’: 3, 
‘resizemode’: ‘noresize’, ‘stretchfactor’: (1.0, 1.0), ‘shearfactor’: 0.0, ‘outline’: 1, ’tilt’: 0.0}
 

Example 2 :

Python3




# import package
import turtle
 
# check default to compare
print(turtle.pen())
 
# update with some inputs
turtle.pen(pencolor="red", outline=2)
 
# again check
print(turtle.pen())


Output :

{‘shown’: True, ‘pendown’: True, ‘pencolor’: ‘black’, ‘fillcolor’: ‘black’, ‘pensize’: 1, ‘speed’: 3, 
‘resizemode’: ‘noresize’, ‘stretchfactor’: (1.0, 1.0), ‘shearfactor’: 0.0, ‘outline’: 1, ’tilt’: 0.0} 
{‘shown’: True, ‘pendown’: True, ‘pencolor’: ‘red’, ‘fillcolor’: ‘black’, ‘pensize’: 1, ‘speed’: 3, 
‘resizemode’: ‘noresize’, ‘stretchfactor’: (1.0, 1.0), ‘shearfactor’: 0.0, ‘outline’: 2, ’tilt’: 0.0}
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads