python - PyOpenGL: glutTimerFunc 回调缺少必需的参数 'value'

标签 python python-3.x opengl pyopengl opengl-compat

使用 PyOpenGL,我尝试创建一个简单的三角形,该三角形在每次刷新调用时都会更改颜色,并且我尝试使用 glutTimerFunc(...) 来执行此操作。

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

import random

def display(value):
    v1 = [-.25,0]
    v2 = [0,.25]
    v3 = [.25, 0]
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    glClear(GL_COLOR_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-1,1,-1,1,-1,1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glBegin(GL_TRIANGLES)

    glColor3b(r,g,b)
    glVertex2f(v1[0], v1[1])
    glVertex2f(v2[0], v2[1])
    glVertex2f(v3[0], v3[1])

    glEnd()
    glFlush()

    glutTimerFunc(1000, display, 5)
    glutPostRedisplay()

if __name__ == '__main__':
    glutInit()
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitWindowSize(500,500)
    glutCreateWindow("First Shape")
    glutDisplayFunc(display)
    glutMainLoop()

这是我遇到的错误...

Traceback (most recent call last):
File "_ctypes/callbacks.c", line 232, in 'calling callback function'
TypeError: display() missing 1 required positional argument: 'value'

我尝试传递“值”并将其放入 display() 的参数列表中,但没有成功。

最佳答案

glutDisplayFunc所需的回调函数无参数,但 glutTimerFunc 需要的回调函数有 on 参数。所以你不能对两者使用相同的函数。
类似以下内容是可能的:

def timer(value):
    display()

def display()

    glutTimerFunc(1000, timer, 5)

但这并不能解决你的问题,因为 glutPostRedisplay将当前窗口标记为需要重新显示,并导致显示回调函数,由 glutDisplayFunc 设置,称为。

如果您想按时间更改颜色,则创建一个计时器函数,该函数设置新颜色并重新启动计时器:

r = 255
g = 255
b = 255

def timer(value):
    global r, b, g
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    glutTimerFunc(1000, timer, 5)

display 函数中删除计时器初始化

def display():
    global r, b, g
    v1 = [-.25,0]
    v2 = [0,.25]
    v3 = [.25, 0]
    glClear(GL_COLOR_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-1,1,-1,1,-1,1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glBegin(GL_TRIANGLES)

    glColor3b(r,g,b)
    glVertex2f(v1[0], v1[1])
    glVertex2f(v2[0], v2[1])
    glVertex2f(v3[0], v3[1])

    glEnd()
    glFlush()

    glutPostRedisplay()

但是在启动时调用timer函数一次:

if __name__ == '__main__':
    glutInit()
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
    glutInitWindowSize(500,500)
    glutCreateWindow("First Shape")
    glutDisplayFunc(display)
    timer(0);
    glutMainLoop()

关于python - PyOpenGL: glutTimerFunc 回调缺少必需的参数 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291221/

相关文章:

python - 如何在 Python 中不定义变量

python - 如何修复 Python 中的 "Element not interactable"Selenium 错误?

python - 延迟库允许打包函数访问多少信息?

python - 在 Google App Engine 上使用 localflavor Django 表单字段

python - 记录命名元组的标准方法是什么?

python - 在 Python 3 中使用 SAX 解析器解析 XML

OpenGL - 同一对象的不同表面上的不同纹理

opengl - 绑定(bind) FBO(帧缓冲对象)的成本有多高

opengl - 查看玩家正在看什么 "block"

javascript - django select2 插件中所有选定项目的列表