PyOpenGL:glutTimerFunc 回调缺少必需的参数 'value'
PyOpenGL: glutTimerFunc callback missing required param 'value'
使用 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'
我试过传递 'value' 并将其放入 display() 的参数列表中,但没有成功。
glutDisplayFunc
has no paramter, but the callback function required by glutTimerFunc
要求的回调函数有on参数。所以你不能对两者使用相同的功能。
类似以下内容是可能的:
def timer(value):
display()
def display()
glutTimerFunc(1000, timer, 5)
但这并不能解决你的问题,因为
glutPostRedisplay
marks the current window as needing to be redisplayed and causes that the display callback function, which is set by 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()
使用 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'
我试过传递 'value' 并将其放入 display() 的参数列表中,但没有成功。
glutDisplayFunc
has no paramter, but the callback function required by glutTimerFunc
要求的回调函数有on参数。所以你不能对两者使用相同的功能。
类似以下内容是可能的:
def timer(value):
display()
def display()
glutTimerFunc(1000, timer, 5)
但这并不能解决你的问题,因为
glutPostRedisplay
marks the current window as needing to be redisplayed and causes that the display callback function, which is set by 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()