有没有办法在 Python3.8 中使用 rgb 颜色?

Is there a way to have rgb colors in Python3.8?

我试图指定 RGB 颜色以使程序生成随机颜色。然而,我正在尝试如何处理 RGB 颜色,它会显示一个错误,它有错误的颜色输入,更具体地说,"raise TurtleGraphicsError("错误的颜色序列:%s" % str(color)) turtle.TurtleGraphicsError:颜色顺序错误:(0, 255, 0)"

from random import randint

myPen = turtle.Turtle()
myPen.ht()
myPen.speed(0)
myPen.pencolor(0,255,0)
myPen.begin_fill()
points = [[-500,-400],[0,500],[500,-400]] #size of triangle
def getMid(p1,p2):
    return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2) #find midpoint

def triangle(points,depth):

    myPen.up()
    myPen.goto(points[0][0],points[0][1])
    myPen.down()
    myPen.goto(points[1][0],points[1][1])
    myPen.goto(points[2][0],points[2][1])
    myPen.goto(points[0][0],points[0][1])

    if depth>0:
        z=(randint(0,255),randint(0,255),randint(0,255))
        myPen.fillcolor(z)
        triangle([points[0],
                        getMid(points[0], points[1]),
                        getMid(points[0], points[2])],
                   depth-1)
        triangle([points[1],
                        getMid(points[0], points[1]),
                        getMid(points[1], points[2])],
                   depth-1)
        triangle([points[2],
                         getMid(points[2], points[1]),
                         getMid(points[0], points[2])],
                   depth-1)



triangle(points,7)

我发现您需要一个 window(或任何变量)=turtle.Screen()。然后你可以做 window.colormode(255) 然后你可以改变配色方案

你的问题和你自己的回答暗示了一些不正确的事情。 Python乌龟默认是RGB。它不是 整数 颜色值。默认为 RGB 浮动 值:

from turtle import Screen
from random import random

color = random(), random(), random()

screen = Screen()
screen.bgcolor(color)
screen.exitonclick()

使用 colormode(),您将在浮点和整数 RGB 值之间切换。

您还可以切换到其他颜色模型,例如 HSB,方法是加载额外的 Python 库并将这些颜色模型转换为 RGB for turtle。