Python返回另一个函数

问题描述 投票:2回答:3

我正在与PyOpenGL合作,我想制作小型RGB转换器功能。用于色彩的PyOpenGL使用从0到1的浮点数。所以0 0 0-黑色1 1 1-白色。我做这个:

def RGB(Red, Green, Blue):
    Red = 1 / 255 * Red
    Green = 1 / 255 * Green
    Blue = 1 / 255 * Blue
    return Red, Green, Blue

并且我这样使用他:

glClearColor(RGB(255, 255, 255), 1)

但是我得到错误:

this function takes 4 arguments (2 given)

我不明白如何返回多个参数

python return pyopengl
3个回答
2
投票

您的函数RGB返回元组(1.0, 1.0, 1.0),您可以使用asterisk(*) operator打开其包装,例如glClearColor(*RGB(255, 255, 255), 1)


1
投票

您的函数glClearColor需要4个参数,您传递了两个参数:元组和整数。

您想使用*运算符打开元组的包装:

def RGB(Red, Green, Blue):
    Red = 1 / 255 * Red
    Green = 1 / 255 * Green
    Blue = 1 / 255 * Blue
    return Red, Green, Blue

def glClearColor(a, b, c, d):
    print(a, b, c, d)

glClearColor(*RGB(255, 255, 255), 1)
# 1.0 1.0 1.0 1

1
投票

问题不在于您的RGB函数,而是您如何调用glClearColor

[您的RGB函数返回一个3元组,这意味着glClearColor(RGB(255, 255, 255), 1)用一个元组和glClearColor调用1(2个参数,如错误所示)。

您可以使用*将3元组扩展为3个独立的参数:

glClearColor(*RGB(255, 255, 255), 1)

这样,元组的每个元素分别调用glClearColor + 1(总共4个参数)。

相当于:

r, g, b = RGB(255, 255, 255)
glClearColor(r, g, b, 1)
© www.soinside.com 2019 - 2024. All rights reserved.