It is easy to draw color filled shapes in Python Turtle. You can do so using the begin_fill() and end_fill() functions. These two functions are used to enclose a set of Python Turtle commands that will draw a filled shape using the current fill color. So if the current pen color is blue, then any shape you draw will be filled with the color blue. If the pen is currently red, then any shape drawn will be filled with red. Let’s see some examples of how to draw color filled shapes with Turtle now.
Draw A Rectangle
First we need a shape to work with that we can fill with color. We can draw a rectangle to get started.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(10)
def draw_rectangle(linecolor, length1=100, length2=150):
color(linecolor)
for i in range(2):
forward(length1)
left(90)
forward(length2)
left(90)
draw_rectangle('blue')
done()
Fill Rectangle With Color
That’s a pretty nice rectangle! Now we want to fill it in with color, and we can do so by adding the begin_fill() and end_fill() functions before and after the call to draw_rectangle().
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(10)
def draw_rectangle(linecolor, length1=100, length2=150):
color(linecolor)
for i in range(2):
forward(length1)
left(90)
forward(length2)
left(90)
begin_fill()
draw_rectangle('blue')
end_fill()
done()
Draw color filled square in Python Turtle
Now that we know how to use begin_fill() and end_fill() let’s draw a square and fill it with a color.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(10)
def draw_square(linecolor, length=150):
color(linecolor)
for i in range(4):
forward(length)
left(90)
begin_fill()
draw_square('red')
end_fill()
done()
Draw Multiple Shapes With Different Colors
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(20)
def draw_triangle(linecolor, length=50):
color(linecolor)
begin_fill()
for i in range(3):
forward(length)
left(120)
end_fill()
up()
setx(100)
down()
draw_triangle('red')
right(180)
forward(100)
right(180)
draw_triangle('green', 100)
right(180)
forward(200)
right(180)
draw_triangle('blue', 200)
done()
Draw color filled octagon in Python Turtle
Here is a Python program to draw a color filled octagon in turtle programming.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(20)
def draw_octagon(linecolor):
color(linecolor)
begin_fill()
for i in range(8):
left(45)
forward(50)
end_fill()
draw_octagon('purple')
done()
Draw color filled Star in Python Turtle
This Python program will draw a color filled Star using turtle.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(20)
def draw_star(linecolor):
color(linecolor)
begin_fill()
for i in range(5):
forward(200)
right(144)
end_fill()
draw_star('violet')
done()
Draw a colored filled half-circle in python turtle
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
width(5)
color('orange')
begin_fill()
circle(100, 180)
end_fill()
hideturtle()
done()
In this tutorial, we saw how easy it is to draw various shapes with Python turtle and apply filled colors to them. We are able to draw color filled shapes with the begin_fill() and end_fill() functions provided in the Turtle library.