2011-01-13 37 views
1

我正嘗試在使用GLUT的Python(v2.7)程序中創建一個右鍵單擊彈出式菜單。我還沒有找到Python特定的文檔來做這件事,所以我使用了C++文檔,這通常幾乎相似。在Python-OpenGL中創建GLUT彈出式菜單

這是我有:

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

這裏是創建菜單的功能:

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 

菜單被正常顯示,但是當我點擊一個項目,我得到這個錯誤:

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 

python-opengl是否有不同的方式做到這一點?我在這裏做錯了什麼?

謝謝。

回答

1

不幸的是,PyOpenGL定義了回調函數的方式,它期望一個int作爲返回類型,而不是無效的。下面是您應該工作的CreateMenu回調函數的更新版本。

def CreateMenu(): 
    menu = glutCreateMenu(processMenuEvents) 
    glutAddMenuEntry("One", 1) 
    glutAddMenuEntry("Two", 2) 
    glutAttachMenu(GLUT_RIGHT_BUTTON) 
    # Add the following line to fix your code 
    return 0