【问题标题】:what does bad color sequence mean in python turtle?python turtle 中的坏颜色序列是什么意思?
【发布时间】:2013-05-22 14:44:20
【问题描述】:

我正在将 python turtle 用于需要海龟来绘制字符的项目。但是,当我尝试将 RGB 值用于颜色时,我不断收到错误消息。输入是:

turtle.color((151,2,1))

接着是一系列动作。但是,当我运行该程序时,我会收到以下消息:

File "C:/Users/Larry/Desktop/tests.py", line 5, in center
turtle.color((151,2,1))
File "<string>", line 1, in color
File "C:\Python33\lib\turtle.py", line 2208, in color
pcolor = self._colorstr(pcolor)
File "C:\Python33\lib\turtle.py", line 2688, in _colorstr
return self.screen._colorstr(args)
File "C:\Python33\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
turtle.TurtleGraphicsError: bad color sequence: (151, 2, 1)

这是什么意思,我该如何解决?

【问题讨论】:

  • 运行screen.colormode(255),它应该可以工作。

标签: python python-3.x turtle-graphics


【解决方案1】:

如果您使用函数生成随机颜色并且使用 random 模块然后返回 3 个随机整数的元组并传递此元组颜色函数,则会出现此错误 你可以通过编写这个 turtle.colormode(255) 来解决这个问题

使用此代码

import random
import turtle

from turtle import Turtle
turtle.colormode(255)
tim = Turtle()
tim.speed("fastest")
direction = [90,  270]
#colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
tim.pensize(5)

def random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    random_color = (r, g, b)
    return random_color


while True:
    move = random.choice(direction)
    tim.forward(40)
    tim.color(random_color())
    tim.left(move)

【讨论】:

    【解决方案2】:

    如果您正在使用函数生成随机颜色并且您正在使用 random 模块然后返回 3 个随机整数的元组并传递此元组颜色函数,则会出现此错误 你可以通过编写这个 turtle.colormode(255) 来解决这个问题

    使用此代码

    import random
    import turtle
    
    from turtle import Turtle
    turtle.colormode(255)
    tim = Turtle()
    tim.speed("fastest")
    direction = [90,  270]
    #colours = ["CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue", "LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
    tim.pensize(5)
    
    def random_color():
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        random_color = (r, g, b)
        return random_color
    
    
    while True:
        move = random.choice(direction)
        tim.forward(40)
        tim.color(random_color())
        tim.left(move)
    

    【讨论】:

      【解决方案3】:

      #尝试导入colormode函数

      从海龟进口海龟 从乌龟进口颜色模式 从海龟导入屏幕

      tim = Turtle()
      screen = Screen()
      colormode(255)
      
      tim.pencolor(151,2,1)
      screen.exitonclick()
      

      【讨论】:

        【解决方案4】:

        一个非常简短的回答是,这意味着传递给 pencolor() 方法的值之前没有通过 Screen 对象方法 colormode() 设置。

        必须创建一个屏幕对象。然后,必须设置颜色模式。因此,使海龟笔可以接受包含 0 到 255 范围内数字的元组类对象。(255, 0, 20) 例如。为什么?因为设置颜色模式的方法不止一种。

        例如

        from turtle import Turtle
        from turtle import Screen
        
        # Creating a turtle object
        bert = Turtle()
        
        # Creating the screen object
        screen = Screen()
        
        # Setting the screen color-mode
        screen.colormode(255)
        
        # Changing the color of the pen the turtle carries
        bert.pencolor(255, 0, 0)
        
        # 'Screen object loop to prevent the window from closing without command'
        screen.exitonclick()
        

        【讨论】:

          【解决方案5】:

          来自docs

          r、g 和 b 中的每一个都必须在 0..colormode 范围内,其中 colormode 为 1.0 或 255(请参阅 colormode())。

          您的颜色模式可能设置为 1.0,因此单个颜色坐标需要在 0 到 1 范围内浮动,或者您需要将颜色模式设置为 255。

          【讨论】:

            猜你喜欢
            • 2020-11-02
            • 1970-01-01
            • 2020-02-21
            • 1970-01-01
            • 2020-12-01
            • 2013-05-19
            • 1970-01-01
            • 2011-05-14
            • 1970-01-01
            相关资源
            最近更新 更多