turtle.Screen().setup() method - Python
Last Updated :
22 Mar, 2025
setup() method in Python's turtle module is used to set up the size and position of the turtle graphics window before drawing. By default, the turtle window appears in the center of the screen with a standard size, but setup() allows customization.
Example: Setting a Fixed Window Size
Python
import turtle
sc = turtle.Screen()
sc.setup(width=600, height=400) # Window size: 600x400 pixels
sc.bgcolor("pink")
turtle.done()
Output:
Explanation: Here, we create a turtle graphics screen and set its dimensions to 600 pixels wide and 400 pixels high. The bgcolor("pink") function changes the background color to pink. The turtle.done() function ensures the window remains open.
Syntax of setup()
turtle.Screen().setup(width=None, height=None, startx=None, starty=None)
Parameters:
Parameter | Description |
---|
width | Defines the window's width in pixels or as a fraction of the screen width. |
---|
height | Sets the window's height in pixels or as a fraction of the screen height. |
---|
startx | Specifies the x-coordinate for the window's top-left corner (in pixels). |
---|
starty | Specifies the y-coordinate for the window's top-left corner (in pixels). |
---|
Note:
- width and height can be given as absolute values (pixels) or as fractions of the screen size (e.g., 0.5 means 50% of the screen).
- startx and starty define the position of the window’s top-left corner. If not provided, the window is centered.
Examples of turtle.Screen().setup()
Example 1: Setting Window Size and Position with Color
Python
import turtle
screen = turtle.Screen()
screen.setup(width=500, height=300, startx=100, starty=150)
screen.bgcolor("lightblue") # Set background color
turtle.done()
Output:
Explanation: In this example, we set the window size to 500x300 pixels and position it at (100,150) on the screen. The startx=100 and starty=150 parameters define the top-left corner’s coordinates. The background color is set to light blue.
Example 2: Using Fractions of the Screen Size with Color
Python
import turtle
screen = turtle.Screen()
screen.setup(width=0.5, height=0.5) # 50% of screen width and height
screen.bgcolor("lightgreen") # Set background color
turtle.done()
Output:
Explanation: Here, the width and height are set to 0.5, meaning the window occupies 50% of the total screen size in both dimensions. This makes it adaptable to different screen resolutions. The background color is set to light green.