turtle.screensize() function in Python
The turtle.screensize() function in Python's turtle module is used to set or retrieve the size of the drawing canvas (the area where the turtle moves and draws). This function helps in customizing the workspace according to user requirements.
Example: Checking default screen size
import turtle
# Create screen object
screen = turtle.Screen()
# Get the current screen size
print(screen.screensize())
Output
(400, 300)
Explanation: Turtle.Screen() object initializes the drawing window and screensize() retrieves the default canvas size, typically (400, 300) pixels, when called without arguments.
Syntax of turtle.screensize()
turtle.screensize(canvwidth=None, canvheight=None, bg=None)
Parameters:
Arguments | Value | Description |
---|---|---|
canvwidth | positive integer | new width of canvas in pixels |
canvheight | positive integer | new height of canvas in pixels |
bg | color string or tuple | new backgroundcolor |
Return Value: If no arguments are passed, turtle.screensize() returns a tuple (canvaswidth, canvasheight), representing the current canvas dimensions.
Examples of turtle.screensize()
Example 1: Setting a Custom Screen Size
import turtle
# Create screen object
screen = turtle.Screen()
# Set a custom screen size
screen.screensize(600, 400, "lightblue")
# Create turtle
t = turtle.Turtle()
t.pensize(5)
t.forward(200)
turtle.done()
Output

Explanation: Screen object is created and the canvas size is set to 600×400 pixels with a light blue background. A Turtle object is then initialized with a pen size of 5. The turtle moves forward 200 pixels, drawing a line. Finally, turtle.done() keeps the window open.
Example 2: Drawing a Pattern with a Set Screen Size
# import package
import turtle
# set turtle
turtle.width(2)
turtle.speed(10)
# loop for pattern
for i in range(10):
turtle.circle(40)
turtle.right(36)
# set screen and drawing remain as it is.
turtle.screensize(canvwidth=400, canvheight=300,
bg="blue")
Output

Explanation: Turtle's pen width is 2 and speed 10 for faster execution. A loop runs 10 times, drawing 40-radius circles while rotating 36 degrees to form a pattern. The screen size is then set to 400×300 with a blue background. turtle.done() keeps the window open.
Example 3: Retrieving Screen Size Dynamically
import turtle
# Create screen object
screen = turtle.Screen()
# Set custom screen size
screen.screensize(800, 500, "green")
# Get and print current screen size
print(screen.screensize())
turtle.done()
Output
(800,500)

Explanation: Screen object is created, and the size is set to 800×500 pixels with a green background. screensize() retrieves the current dimensions, which are then printed. turtle.done() keeps the window open.