2016-11-14 65 views
0

我把一些wxPython/PyOpenGL代碼用於運行,但不再運行 - 顯然它在一個模塊或另一個模塊更新之後停止工作,我不知道何時。爲什麼這個wxPython/PyOpenGL代碼在glPushMatrix上引發錯誤?

下面是一個重現錯誤的小例子應用程序。

import wx 
from wx import glcanvas 
from OpenGL.GLUT import * 
from OpenGL.GL import * 

class WxMyTestApp(wx.App): 
    def __init__(self): 
    wx.App.__init__(self) 
    def OnInit(self): 
    self.viewFrame = wx.Frame(None, -1, 'viewFrame', wx.DefaultPosition, (400,400)) 
    self.canvas = glcanvas.GLCanvas(self.viewFrame) 
    self.canvas.Bind(wx.EVT_PAINT, self.OnPaint) 
    return True 
    def OnPaint(self, ev): 
    print "OnPaint ", 
    dc = wx.PaintDC(self.canvas) 
    self.canvas.SetCurrent() 
    self.redraw(ev) 
    def redraw(self, ignoredEvent): 
    glPushMatrix() 
    glColor3f(1.0, 1.0, 0.0) 
    glLineWidth(1) 
    glutWireSphere(0.5, 10,10) 
    glPopMatrix() 
    glFlush() 
    self.canvas.SwapBuffers() 

if __name__ == '__main__': 
    d = WxMyTestApp() 
    d.viewFrame.Show() 
    d.MainLoop() 

(請注意我用的wx.App代替wx.PySimpleApp因爲我真正的代碼使用多個窗口。)運行測試代碼,該框架顯示左上角一個白色的小矩形在調整大小給出了這樣重複的錯誤:

Traceback (most recent call last): 
    File "myTestApp.py", line 18, in OnPaint 
    self.redraw(ev) 
    File "myTestApp.py", line 20, in redraw 
    glPushMatrix() 
    File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__ 
    return self(*args, **named) 
    File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\error.py", line 232, in glCheckError 
    baseOperation = baseOperation, 
OpenGL.error.GLError: GLError(
     err = 1282, 
     description = 'invalid operation', 
     baseOperation = glPushMatrix, 
     cArguments =() 

是否真的在這裏做一個推一個問題嗎?我懷疑這個問題與GL設備環境有關,但不知道如何調試它。 Python 2.7.10,wxPython 3.0.2.0-3,PyOpenGL 3.1.0-2(Canopy發行版)。

回答

0

我受益於wxWidgets/wxPython Phoenix努力下明顯產生的GLCanvasGLContext的文檔。似乎早期版本的wxWidgets會隱式地爲您創建一個GLContext,但這已被棄用。使用這些信息,我能夠獲得最新的代碼。我需要這兩個變化是:這行

self.canvas = glcanvas.GLCanvas(self.viewFrame) 
self.context = glcanvas.GLContext(self.canvas) # add this line 

然後在OnPaint變化:

OnInit,在轉讓之後添加一行self.canvas

self.canvas.SetCurrent() 

這樣:

self.canvas.SetCurrent(self.context) # add the argument 

這些小小的改變讓我的代碼變得更好再次運行。希望別人能從中受益。