How to add background color in Python turtle?

by ernestina , in category: Python , 2 years ago

How to add background color in Python turtle?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by kennedi , 2 years ago

@ernestina  It's easy just remember this, turtle.Screen and turtle.bgcolor() :

1
2
3
import turtle
turtle.Screen()
turtle.bgcolor('black')


Member

by berta , a year ago

@ernestina 

You can add background color in Python turtle using the bgcolor() method. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import turtle

# create turtle screen
screen = turtle.Screen()

# set background color
screen.bgcolor("lightblue")

# create turtle
t = turtle.Turtle()

# draw a square
for i in range(4):
    t.forward(100)
    t.right(90)

# exit on click
screen.exitonclick()


In this example, we set the background color of the turtle screen to light blue using the bgcolor() method. You can replace "lightblue" with any valid color name or hexadecimal code to change the background color.