将RGB值输入到bgcolor()时,Python乌龟引发错误

时间:2018-10-22 15:43:37

标签: python turtle-graphics

我的代码抛出一个错误,总结为:

“ Turtle中的颜色内置函数无法同时使用Python的内置函数和整数来确定颜色值时使用的不等式”

from turtle import *

pen = Turtle()
screen = Screen()
screen.colormode(255)
screen.bgcolor('white')

def pen_color(i):
    print("Input a number between 1 and 255 for red")
    red = input
    print("Input a number between 1 and 255 for green")
    green = input
    print("Input a number between 1 and 255 for blue")
    blue = input
    screen.bgcolor(red, green, blue)

pen_color(pen)
pen.exitonclick()

1 个答案:

答案 0 :(得分:0)

您的代码有几个问题:@PatrickHaugh指出:

print("Input a number between 1 and 255 for red")
red = input()

不会做您想要/期望的事情。要么做:

red = input("Input a number between 1 and 255 for red")

或更妙的是:

exitonclick()

接下来,这将无法工作,因为ScreenTurtle而非pen.exitonclick() 的方法:

bgcolor()

最后,您遇到颜色问题的原因是screen.bgcolor(int(red), int(green), int(blue)) 需要数字参数,并且您将其传递给字符串。您需要这样做:

from turtle import Screen, Turtle

def pen_color():
    red = input("Input a number between 1 and 255 for red: ")
    green = input("Input a number between 1 and 255 for green: ")
    blue = input("Input a number between 1 and 255 for blue: ")

    screen.bgcolor(int(red), int(green), int(blue))

screen = Screen()
screen.colormode(255)

pen_color()

pen = Turtle()

screen.exitonclick()

将所有内容放在一起:

<Element Where="Time=3,Successful=N"></Element>