Hi guys, I’m a beginner with wxPython and pyOpenGL. Here is a problem I met: when I run the code below, the GLCanvas always shows a black background, I tried to change glClearColor()
, but not works. Please point out where my code is wrong, thank you.
"""
Hello World, but with more meat.
"""
import wx
from wx.glcanvas import GLCanvas, GLContext
from OpenGL.GL import *
class GLView(GLCanvas):
def __init__(self, parent):
GLCanvas.__init__(self, parent, size=(200, 200))
# self.Bind(wx.EVT_ERASE_BACKGROUND, self.processEraseBackgroundEvent)
# self.Bind(wx.EVT_SIZE, self.processSizeEvent)
# self.OnInitGL()
self.context = GLContext(self)
self.SetCurrent(self.context)
# self.Bind(wx.EVT_PAINT, self.processPaintEvent)
self.OnDraw()
# def OnInitGL(self):
# glClearColor(0, 0, 1, 1)
# glClear(GL_COLOR_BUFFER_BIT)
#
# def processPaintEvent(self, event):
# self.SetCurrent(self.context)
# self.OnDraw()
# event.Skip()
def OnDraw(self):
# glClear(GL_COLOR_BUFFER_BIT)
# # glPointSize(3.0)
#
glClearColor(0, 0, 1, 1)
glColor3f(1.0, 1.0, 0.0)
glBegin(GL_LINES)
glVertex2f(-5.0, 0.0)
glVertex2f(5.0, 0.0)
glVertex2f(0.0, 5.0)
glVertex2f(0.0, -5.0)
glEnd()
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINES)
# for x in arange(-5.0, 5.0, 0.1):
for x in (i * 0.1 for i in range(-50, 50)):
y = x * x
glVertex2f(x, y)
glEnd()
self.SwapBuffers()
self.Refresh()
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
# self.panel = ExamplePanel(self)
glview = GLView(self)
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
# main_sizer.Add(self.panel)
main_sizer.Add(glview)
self.SetSizerAndFit(main_sizer)
# self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MainFrame(None)
app.MainLoop()