turtle.color() function in Python

The turtle.color() function sets both the pen color and the fill color to the given color. Can set them to the same value with one argument.

Syntax: turtle.color(*args)
Parameters: Can be called as color(colorstring), color((r, g, b)), or color(colorstring1, colorstring2) to set pencolor and fillcolor separately.

Let us see an example to implement the turtle.color() function in Python:

Demo24.py

# turtle.color() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.color("red")          # Set both pen and fill color to red
t.forward(100)
t.color("blue", "green") # Set pen color to blue, fill color to green
t.forward(100)

window.exitonclick()

The following is the output:

turtle.color() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
  4. Set both colors to red: t.color(“red”) sets both the pen (outline) color and the fill color to red.
  5. Draw first segment: t.forward(100) draws a 100-unit red line (pen is down by default).
  6. Set separate pen/fill colors: t.color(“blue”, “green”) sets the pen color to blue and the fill color to green.
  7. Draw second segment: t.forward(100) draws a 100-unit blue line; the fill color is now green for any future fill operations.
  8. Note on fill: The fill color affects shapes only when used with begin_fill() and end_fill().
  9. Close on click: window.exitonclick() keeps the window open until you click, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.write() function in Python
turtle.fillcolor() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment