When using the turtle module in Python, you can Use the fillcolor() function to define the fill color of a shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

import turtle

t = turtle.Turtle()

t.fillcolor("blue")

t.begin_fill()

t.circle(50)

t.end_fill()

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw circles or draw squares easily in Python with the turtle module.

When working with the turtle module, sometimes it makes sense to want to change the fill color of our shapes.

To fill a shape, there are a few steps to take. We use the fillcolor() function to define the fill color of our shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

Below is an example and the output of how to fill a circle with the color “blue” using fillcolor(), begin_fill() and end_fill() in Python.

import turtle

t = turtle.Turtle()

t.fillcolor("blue")

t.begin_fill()

t.circle(50)

t.end_fill()

turtle rgb colors circle

Generating a Random Color Turtle with Python turtle Module

When creating graphics, sometimes it’s cool to be able to generate random colors to make random colored shapes or designs.

We can generate random colors using RGB colors. To use RGB colors, we change the color mode to RGB mode (‘255’), and then we use the randint() function from the random module to generate random numbers in the range 0 to 255.

Below is an example of how to use Python to get a random fill color to draw a triangle and then fill it with the fillcolor() function.

import turtle
from random import randint

turtle.colormode(255)

t = turtle.Turtle()

t.pencolor(randint(0,255),randint(0,255),randint(0,255))

t.fillcolor(randint(0,255),randint(0,255),randint(0,255))

t.begin_fill()

def draw_triangle(side_length):
    for i in range(0,3):
        t.forward(side_length)
        t.right(120)

draw_triangle(100)

t.end_fill()

random colors triangle turtle

Hopefully this article has been useful for you to learn how to fill a shape using the turtle module with the fillcolor() function in Python.

Categorized in:

Python,

Last Update: March 1, 2024