how to use glutKeyboardFunc in python?

Question:

i just wanna use my keyboard for transform my logo, but doesnt work, did i forget something?
what should i do to get glutKeyboardFunc work? please help me

w,h= 600,600
xScale = 1
yScale = 1

def logo():
    glScaled(xScale,yScale,0)
    glBegin(GL_POLYGON)
    glColor3ub(255,0,0)
    glVertex2f(0, 0)
    glVertex2f(120, 200)
    glVertex2f(0, 400)
    glVertex2f(-120, 200)
    glEnd()

def tes(key, x, y): 
    global yScale
    global xScale
    if key == 32 :
        xScale += 1
        yScale += 1

def iterate():
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-w, w, -h, h, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 0.0, 3.0)
    logo()
    glutSwapBuffers()

glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(w, h)
glutInitWindowPosition(0, 0)
window = glutCreateWindow("glTransform")
glutDisplayFunc(showScreen)
glutKeyboardFunc(tes)
glutIdleFunc(showScreen)
glutMainLoop()

I am a bit lost about what to do to find the error, if someone could enlighten me i would be grateful.

Asked By: Faisal Bahari

||

Answers:

The callback argument of glutKeyboardFunc is a string in byte string. You have to compare the key with a string:

def tes(key, x, y): 
    global yScale
    global xScale
    if key == b' ':
        xScale += 1
        yScale += 1

Alternatively convert the key to its integer representation with ord:

def tes(key, x, y): 
    global yScale
    global xScale
    if ord(key) == 32:
        xScale += 1
        yScale += 1
Answered By: Rabbid76
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.