我正在制作一个Python Turtle项目,它允许我输入3个颜色值,然后将其用作圆的颜色,然后它应该在文本输出中打印颜色的rgb值和十六进制值。 这是代码:
import turtle
from turtle import colormode
import random
import time
turtle.speed(0)
def randomchoi():
turtle.colormode(255)
randr = random.randint(0, 255)
randg = random.randint(0, 255)
randb = random.randint(0, 255)
rand = (randr, randg, randb)
turtle.color(rand)
turtle.begin_fill()
turtle.left(180)
turtle.forward(55)
turtle.left(135)
turtle.clear()
turtle.circle(100)
turtle.end_fill()
print(rand)
hexrand = '#%02x%02x%02x' % (rand)
print(hexrand)
def rgbselect():
turtle.colormode(255)
rgbr = input("Red = ")
rgbg = input("Green = ")
rgbb = input("Blue = ")
rgb = (rgbr, rgbg, rgbb)
turtle.color(rgb)
turtle.begin_fill()
turtle.left(180)
turtle.forward(55)
turtle.left(135)
turtle.clear()
turtle.circle(100)
turtle.end_fill()
print(rgb)
hexrgb = '#%02x%02x%02x' % (rgb)
print(hexrgb)
rgbselect()
忽略 randchoi,因为它按我想要的方式工作,但我的问题的焦点是“rgbselect”。 该代码一直有效,直到乌龟颜色更改为元组 rgb 我能做什么?
我尝试直接从海龟导入颜色模式,但没有什么区别。
我还尝试使用以下方法将 rgbr、rgbg 和 rgbb 更改为整数:
import turtle
from turtle import colormode
import random
import time
turtle.speed(0)
def randomchoi():
turtle.colormode(255)
randr = random.randint(0, 255)
randg = random.randint(0, 255)
randb = random.randint(0, 255)
rand = (randr, randg, randb)
turtle.color(rand)
turtle.begin_fill()
turtle.left(180)
turtle.forward(55)
turtle.left(135)
turtle.clear()
turtle.circle(100)
turtle.end_fill()
print(rand)
hexrand = '#%02x%02x%02x' % (rand)
print(hexrand)
def rgbselect():
turtle.colormode(255)
rgbr = input("Red = ")
rgbg = input("Green = ")
rgbb = input("Blue = ")
**int(rgbr)
int(rgbg)
int(rgbb)**
rgb = (rgbr, rgbg, rgbb)
turtle.color(rgb)
turtle.begin_fill()
turtle.left(180)
turtle.forward(55)
turtle.left(135)
turtle.clear()
turtle.circle(100)
turtle.end_fill()
print(rgb)
hexrgb = '#%02x%02x%02x' % (rgb)
print(hexrgb)
rgbselect()
它并没有改变结果,我也尝试做 int(rgb) 但出现了不同的错误
第二个版本可能是正确的,但您需要将每个
int(rgbr)
、int(rgbg)
、int(rgbb)
更改为:
rgbr = int(rgbr)
rgbg = int(rgbg)
rgbb = int(rgbb)
如果没有有关错误的任何进一步信息,我无法提供进一步帮助。