Python Turtle pen colour

Brennan Wilkes picture Brennan Wilkes · Dec 2, 2013 · Viewed 31.8k times · Source

When I call t.pencolor('83, 58, 27') (turtle is imported as t) I get the TurtleGraphicsError: bad color string: 83, 58, 27 even though I have (I think) changed my colour mode.

t.colormode(255)    
t.pencolor('83, 58, 27')

I run python 2.7 on OS 10.9

Answer

Burhan Khalid picture Burhan Khalid · Dec 2, 2013

You are passing in a string with three colors, where you need to pass the three colors as three separate integer arguments, like this:

t.pencolor(83, 58, 27)

There are multiple ways to use pencolor, from the documentation:

Four input formats are allowed:

pencolor() Return the current pencolor as color specification string or as a tuple (see example). May be used as input to another color/pencolor/fillcolor call.

pencolor(colorstring) Set pencolor to colorstring, which is a Tk color specification string, such as "red", "yellow", or "#33cc8c".

pencolor((r, g, b)) Set pencolor to the RGB color represented by the tuple of r, g, and b. Each of r, g, and b must be in the range 0..colormode, where colormode is either 1.0 or 255 (see colormode()).

pencolor(r, g, b) Set pencolor to the RGB color represented by r, g, and b. Each of r, g, and b must be in the range 0..colormode.

So you can also send in a tuple of your colors, but again they need to be integers not strings:

t.pencolor((83, 58, 27))