turtle.screensize() function in Python

The turtle.screensize() function gets or sets the size of the drawing canvas. The window can be smaller than the canvas, allowing for scrolling.

Syntax: turtle.screensize(canvwidth=None, canvheight=None, bg=None)
Parameters: canvwidth, canvheight – positive integers (canvas size in pixels); bg – colorstring or color-tuple (background color).

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

Demo58.py

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

import turtle

window = turtle.Screen()
# Get or set the canvas size
window.screensize(1000, 800)  # Set canvas size
canvas_width, canvas_height = window.screensize()
print("Canvas size:", canvas_width, "x", canvas_height)

t = turtle.Turtle()
t.forward(100)

window.exitonclick()

The following is the output:

turtle.screensize() 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.
  • Set canvas size:screensize(1000, 800) sets the scrollable canvas area to 1000 units wide and 800 units high.
  • Get canvas size: canvas_width, canvas_height = window.screensize() retrieves the current canvas dimensions.
  • Print canvas size: print(“Canvas size:”, canvas_width, “x”, canvas_height) outputs the canvas width and height to the console.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Move forward:forward(100) draws a 100-unit line (pen down by default).
  • 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.window_width() function in Python
turtle.title() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment