我想写一个函数来做一个圆,但让用户选择那个圆的RGB值。我尝试过使用python的输入函数和turtle的文本输入,但似乎都不起作用。尽管,这可能不是问题所在。非常感谢您的帮助。
import turtle
# my turtle
t = turtle.Turtle()
red = int(turtle.textinput("Color", "Choose a value between 0-255:"))
#green = int(input("choose a second value between 0 -255."))
#blue = int(input("choose a third value between 0 -255."))
# my colorful circle function
def colors(r,g,b):
t.color(r,g,b)
t.fillcolor(r,g,b)
t.begin_fill()
t.circle(100)
t.end_fill()
green = 0
blue = 0
# calling the function
colors(red,green,blue)发布于 2020-11-06 07:08:02
我认为您的代码的主要问题是您使用的RGB值为0- 255,而除非您使用colormode()指定其他值,否则Python附带的turtle.py使用的RGB值为0.0 - 1.0
from turtle import Screen, Turtle
def colors(r, g, b):
turtle.color(r, g, b)
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
screen = Screen()
screen.colormode(255)
red = int(screen.numinput("Red", "Choose a value between 0-255", minval=0, maxval=255))
green = int(screen.numinput("Green", "Choose a second value between 0-255", minval=0, maxval=255))
blue = int(screen.numinput("Blue", "Choose a third value between 0-255", minval=0, maxval=255))
turtle = Turtle()
colors(red, green, blue)
screen.exitonclick()因为您需要数字输入,所以我从textinput()切换到了numinput()。但是,我保留了int()转换,因为numinput()返回float,而颜色函数需要int。
Python的turtle的一些非标准实现假定RGB值为0- 255,但是根据您对textinput()的使用,我假设您使用的是标准的Python3 turtle。如果没有,请在您的问题中说明您使用的是什么(网站) Python。
https://stackoverflow.com/questions/64705643
复制相似问题