turtle.Screen().setup() function in Python

The turtle.Screen().setup() function sets the size and position of the main window. This is a method of the Screen object, not the Turtle.

Syntax: turtle.setup(width=_CFG[“width”], height=_CFG[“height”], startx=_CFG[“leftright”], starty=_CFG[“topbottom”])
Parameters: width, height – integers (size in pixels or a fraction of the screen); startx, starty – integers (window position).

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

Demo51.py

# turtle.Screen().setup() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
window.setup(width=600, height=400)  # Set window size
window.title("Custom Sized Window")

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

window.exitonclick()

The following is the output:

turtle.Screen().setup() 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 window size: window.setup(width=600, height=400) sets the window’s outer size to 600×400 pixels.
  • Set title: window.title(“Custom Sized Window”) changes the window title bar text.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Move forward: t.forward(100) draws a 100-unit line (pen down by default).
  • Close on click: window.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.resetscreen() function in Python
turtle.bgpic() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment