微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取用户输入以更改颜色的功能

如何解决获取用户输入以更改颜色的功能

我想编写一个函数来制作一个圆,但是让用户选择该圆的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,b)
  t.fillcolor(r,b)
  t.begin_fill()
  t.circle(100)
  t.end_fill()

green = 0
blue = 0

# calling the function
colors(red,green,blue)

解决方法

我在您的代码中看到的主要问题是您使用的是0-255的RGB值,而Python随附的turtle.py使用的是0.0-1.0的RGB值,除非您另外指定使用colormode()

from turtle import Screen,Turtle

def colors(r,g,b):
    turtle.color(r,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",maxval=255))
blue = int(screen.numinput("Blue","Choose a third value between 0-255",maxval=255))

turtle = Turtle()

colors(red,green,blue)

screen.exitonclick()

由于您需要数字输入,因此我从textinput()切换到numinput()。但是,我保留了int()的转换,因为numinput()返回了float并且颜色函数想要int

Python的某些非标准实现的Turtle假定RGB值为0-255,但是根据您对textinput()的使用,我假设您正在使用标准的Python 3 Turtle。如果没有,请在您的问题中说明您使用的是什么(网站)Python。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。