Python Turtle pen colour

冷暖自知 提交于 2020-06-27 13:56:43

问题


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


回答1:


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))



回答2:


For me, I think you can delete the apostrophes as it is not a string, but a value. Like this: t.pencolor(83, 58, 27).



来源:https://stackoverflow.com/questions/20320921/python-turtle-pen-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!