• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Change Python Turtle Shape Fill Color with fillcolor() Function

Change Python Turtle Shape Fill Color with fillcolor() Function

June 6, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using Python to Count Number of False in List
  • 2.  Print Object Attributes in Python using dir() Function
  • 3.  Python Get Yesterday’s Date
  • 4.  Using Python to Check If a Number is a Perfect Square
  • 5.  Using Python to Get Home Directory
  • 6.  Using Python to Check if Queue is Empty
  • 7.  Sorting a List of Tuples by Second Element in Python
  • 8.  How to Measure Execution Time of Program in Python
  • 9.  Remove Leading and Trailing Characters from String with strip() in Python
  • 10.  Remove Every Nth Element from List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy