Search code examples
pythonopenglglut

Creating a GLUT popup menu in Python-OpenGL


I'm trying to create a right-click-popup-menu in a Python(v2.7) program using GLUT. I haven't found Python-specific documentation for doing this, so I used C++ documentation, which is usually almost similar.

Here's what I have:

if __name__=="__main__":
    glutInit(sys.argv)
    #...more initialization code...
    createMenu()
    init()
    glutMainLoop()

And here are the functions that create the menu:

def createMenu():
    menu = glutCreateMenu(processMenuEvents)
    glutAddMenuEntry("One", 1)
    glutAddMenuEntry("Two", 2)
    glutAttachMenu(GLUT_RIGHT_BUTTON)

def processMenuEvents(option):
    logging.debug("Menu pressed")
    # not using 'option' right now

The menu gets displayed correctly, but when I click on an item, I get this error:

DEBUG:root:Menu pressed:
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 338, in 'converting callback result'
TypeError: an integer is required
Exception  in <function processMenuEvents at 0x1760b90> ignored

Does python-opengl have a different way of doing this? What am I doing wrong here?

Thanks.


Solution

  • Specifying callback functions via ctypes in Python doesn't quite work like you are expecting. You should use CFUNCTYPE to create the callback function and use the resulting variable as the parameter to glutCreateMenu.

    You will find more details on ctypes and callback functions here: http://docs.python.org/release/2.5.2/lib/ctypes-callback-functions.html