In some cases you might need more than just drawing.
Filling is a big part of drawing and Python’s turtle has very handy Python methods that can be used to fill shapes and patterns with different colors.
In this tutorial we’re going to show you how to use .fillcolor()
, .begin_fill()
and .end_fill()
methods.
Filling a drawing in turtle is pretty simple.
You just need to place your drawing between begin_fill and end_fill commands.
turtle.fillcolor()
turtle.begin_fill()
turtle.end_fill()
If you’ve done it right turtle will fill the drawing with your choice of color once the drawing ends.
Filling a drawing in turtle is pretty simple.
You just need to place your drawing between begin_fill() and end_fill() commands.
turtle.fillcolor()
turtle.end_fill()
If you’ve done it right turtle will fill the drawing with your choice of color once the drawing ends.
This method activates filling operation in turtle. It should be placed before the drawing of a pattern.
turtle.fillcolor()
object before the turtle.begin_fill()
object.turtle.fillcolor()
turtle.end_fill()
If you’ve done it right turtle will fill the drawing with your choice of color once the drawing ends.
Filling a drawing in turtle is pretty simple.
You just need to place your drawing between begin_fill() and end_fill() commands.
turtle.fillcolor()
turtle.end_fill()
If you’ve done it right turtle will fill the drawing with your choice of color once the drawing ends.
We have a fun Python tutorial on how to draw stars. Now, let’s try to draw a star and fill it with color.
img.resize((x, y))
Shortly, a pentagon star can be drawn with 5 lines. Making a turn with 144 degrees of external angle each time will result in a star. So, we need:
import turtle
turtle.begin_fill()
for i in range(5):
turtle.forward(150)
turtle.left(144)
turtle.end_fill()
turtle.exitonclick()
Now let’s apply begin_fill() using turtle, so that our star is filled with a color.
And another example, this time using .fillcolor() to pass a filling color value to turtle.
import turtle
turtle.fillcolor("orange")
turtle.begin_fill()
for i in range(5):
turtle.forward(150)
turtle.left(144)
turtle.end_fill()
turtle.exitonclick()
Let’s try something different. By only adjusting the angle and iteration number we can get a perfect triangle with the same Python loop.
range(3)
only so that it makes 3 iterations since a triangle has 3 sides.import turtle
turtle.fillcolor("pink")
turtle.begin_fill()
for i in range(3):
turtle.forward(150)
turtle.left(120)
turtle.end_fill()
turtle.exitonclick()
Great. Drawing and filling shapes with turtle is so much fun!
Let’s try something a little more advanced. We can spread a few of these triangles around the horizon using random library making them look like colorful tents.
You can also make them look like mountains by choosing more earthy colors.
To achieve this we will use:
turtle.speed(0)
to speed up the drawing.turtle.bgcolor("lightblue")
to create a pleasant backgroundrandom.randint()
method to randomize tent positions around an imaginary horizon.It’s not that complicated but I wanted to explain as much as possible in case this Python tutorial is being used to teach an audience.
Let’s see the code and turtle drawing.
import turtle
import random
turtle.bgcolor("lightblue")
turtle.speed(0)
turtle.hideturtle()
def triangle_maker(i,j):
lst=["red","green","orange", "yellow","pink","brown","green"]
turtle.color("black")
turtle.fillcolor(lst[random.randint(0,6)])
turtle.begin_fill()
for iter in range(3):
turtle.forward(i)
turtle.left(j)
turtle.end_fill()
def tents():
for i in range(1):
triangle_maker(random.randint(65,125),120)
for i in range(6):
turtle.penup()
turtle.setpos(random.randint(-300, 300), random.randint(-20, 0))
turtle.pendown()
tents()
turtle.exitonclick()
Not bad at all!
An important note: About python.color() position here. It matters if you place python.color() before begin_fill() or after begin_fill().
If you place it after begin_fill(), fillcolor() will be ignored (or supressed) and fill color will be the turtle drawing color and there won’t be any edge color.
So feel free to experiment with it, as long as you know that it matters if turtle.color() comes before or begin_fill() comes before you should be good.
Here is an example created with the code above just by changing swapping the .color, begin_fill positions as below.
#Only to demonstrate top three lines,
#which are swapped from previous Python code.
turtle.fillcolor(lst[random.randint(0,6)])
turtle.begin_fill()
turtle.color("black")
for iter in range(3):
turtle.forward(i)
turtle.left(j)
turtle.end_fill()
In this Python Tutorial, we have learned how to fill turtle drawings with color.
We have demonstrated the use of fillcolor(), begin_fill() and end_fill() with examples.
We have also seen a couple of relatively advanced methods to iterate through a list of colors to create shapes with multiple colors and iterate through a range to draw multiple objects.
We hope you enjoyed this Python turtle tutorial. Thank you.