我的 Python 海龟图形代码出现颜色填充错误,但我不明白为什么。方法
t.fillcolor()
应接受 3 个值作为海龟形状的填充颜色的 r、g、b 值,其中 t
是海龟。但是,该方法中的任何三个值都会产生错误。
这就是我书中对
t.fillcolor()
的解释:
这是代码:
from turtle import Turtle,Screen
#might want to use:
#from turtle import *
from random import randint
def hexagon(t, length):
"""Draws a hexagon with the given length."""
for count in range(6):
t.forward(length)
t.left(60)
def radialPattern(t, n, length, shape):
"""Draws a radial pattern of n shapes with the given length."""
for count in range(n):
shape(t, length)
t.left(360 / n)
screen = Screen() #create a screen object
screen.setup(width=600,height=400) #size
screen.bgcolor("lightblue")
screen.title("My turtle")
t = Turtle()
#added code here:
t.fillcolor(20,75,153)
t.begin_fill()
radialPattern(t,5,30,hexagon)
t.end_fill()
screen.exitonclick() #keep the screen open until user closes it
代码执行从第
screen = Screen() #create a screen object
行开始
这是错误:
Traceback (most recent call last):
File "D:\Turtle for final.py", line 25, in <module>
t.fillcolor(20,75,153)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 2289, in fillcolor
color = self._colorstr(args)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 2697, in _colorstr
return self.screen._colorstr(args)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 1167, in _colorstr
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (20, 75, 153)
我已经多次让这种方法正常工作。它在这里不起作用,我不明白为什么。
如果有人可以帮助我,我将不胜感激。
好吧,点击turtle(或其别名)模块并使用turtle.colormode(255) 设置颜色模式,然后您可以使用rgb 元组值。如果你点击构造函数,它将不起作用。如果有帮助,请告诉我。