Using Pyglet and wx.Python together

645 views
Skip to first unread message

Keith

unread,
Jan 29, 2012, 12:42:24 AM1/29/12
to pyglet-users
Does anyone have an example showing how to use Pyglet and wx.Python
together in the same app?

Thanks!

--Keith

Txema Vicente

unread,
Feb 11, 2012, 2:08:55 PM2/11/12
to pyglet...@googlegroups.com
Hi.

I found this example in this list:
http://groups.google.com/group/pyglet-users/browse_thread/thread/230885dcdaee0394/0e3628eef014e3a1?lnk=gst&q=wxpython#0e3628eef014e3a1

And now I'm using something like this, but don't know if it is a good
way to do it.

import wx
import wx.lib.newevent
import pyglet
import threading
import time
from wx import glcanvas
from pyglet import gl

PygletDrawEvent, EVT_PYGLET_DRAW = wx.lib.newevent.NewEvent()

class PygletBatchThread(threading.Thread):
def __init__(self, panel):
self.sleep = 1/50.0
threading.Thread.__init__(self)
self.w, self.h = 0, 0
self.batch = pyglet.graphics.Batch()
self.Init()
self.running = True
self.panel = panel
self.panel.Bind(EVT_PYGLET_DRAW, self.panel.OnDraw)
self.panel.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvent)
self.start()

def run(self):
print "Start"
while self.running:
self.Loop()
try:
wx.PostEvent(self.panel, PygletDrawEvent())
except:
self.running=False
time.sleep(self.sleep)
print "End"

def OnMouseEvent(self, event):
event_type = event.GetEventType()
x,y = event.GetPosition()
y=self.h-y
if event_type == wx.wxEVT_MOTION:
x1, y1, x2, y2 = -32, -32, 32, 32
self.quad.vertices[:] = [x+x1, y+y1, x+x2, y+y1, x+x2,
y+y2, x+x1, y+y2]

elif event_type == wx.wxEVT_LEFT_DOWN:
print "MOUSE %s" % (event.GetPosition())
elif event_type == wx.wxEVT_LEFT_UP: pass
elif event_type == wx.wxEVT_LEFT_DCLICK: pass
elif event_type == wx.wxEVT_MIDDLE_DOWN: pass
elif event_type == wx.wxEVT_MIDDLE_UP: pass
elif event_type == wx.wxEVT_MIDDLE_DCLICK: pass
elif event_type == wx.wxEVT_RIGHT_DOWN: pass
elif event_type == wx.wxEVT_RIGHT_UP: pass
elif event_type == wx.wxEVT_RIGHT_DCLICK: pass
elif event_type == wx.wxEVT_ENTER_WINDOW: pass
elif event_type == wx.wxEVT_LEAVE_WINDOW: pass
elif event_type == wx.wxEVT_MOUSEWHEEL: pass

def Loop(self):
self.c+=1
if self.c==256: self.c=0
self.quad.colors[:] = (128+self.c/2,128-self.c/2,128,255) * 4

def OnResize(self, width, height):
self.w, self.h = width, height
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(0, width, 0, height, 1, -1)
gl.glMatrixMode(gl.GL_MODELVIEW)

def Init(self):
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
gl.glEnable(gl.GL_TEXTURE_2D)
gl.glShadeModel(gl.GL_SMOOTH)
gl.glClearColor(1, 1, 1, 1)

self.quad = self.batch.add(4, gl.GL_QUADS, None,'v2i/dynamic',
'c4B')
self.c = 0

class PygletPanel(wx.Panel):
def __init__(self, parent, id=wx.ID_ANY, style=0):
style = style | wx.NO_FULL_REPAINT_ON_RESIZE
attribList = (glcanvas.WX_GL_RGBA, # RGBA
glcanvas.WX_GL_DOUBLEBUFFER, # Double Buffered
glcanvas.WX_GL_DEPTH_SIZE, 32) # 24 bit
self.GLinitialized = False
super(PygletPanel, self).__init__(parent, id, style=style)
self.Freeze()
self.canvas = glcanvas.GLCanvas(self, attribList=attribList)
s = wx.BoxSizer(wx.HORIZONTAL)
s.Add(self.canvas, 1, wx.EXPAND)
self.SetSizerAndFit(s)
self.canvas.Bind(wx.EVT_ERASE_BACKGROUND, lambda e: 0)
self.canvas.Bind(wx.EVT_SIZE, self.EventSize)
self.canvas.Bind(wx.EVT_PAINT, self.EventPaint)
self.Bind(wx.EVT_CLOSE, self.Destroy)
self.EventSize(None)
self.Thaw()

def EventSize(self, event):
'''Process the resize event.'''
if self.canvas.GetContext():
# Make sure the frame is shown before calling SetCurrent.
self.Show()
self.canvas.SetCurrent()
size = self.canvas.GetClientSize()
self.width, self.height = size.width, size.height
self.OnResize(size.width, size.height)
self.canvas.Refresh(False)
if event is not None: event.Skip()

def EventPaint(self, event):
'''Process the drawing event.'''
self.canvas.SetCurrent()
if not self.GLinitialized:
self.OnInitGL()
self.OnDraw()
event.Skip()

def Destroy(self):
self.thread.stop()
self.pygletcontext.destroy()
super(wx.Panel, self).Destroy()

def OnInitGL(self):
'''Initialize OpenGL for use in the window.'''
#create a pyglet context for this panel
self.pygletcontext = gl.Context(gl.current_context)
self.pygletcontext.set_current()
self.thread = PygletBatchThread(self)
self.GLinitialized = True
size = self.canvas.GetClientSize()
self.OnResize(size.width, size.height)

def OnResize(self, width, height):
if self.GLinitialized:
self.pygletcontext.set_current()
self.thread.OnResize(width, height)
else:
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glOrtho(0, width, 0, height, 1, -1)
gl.glMatrixMode(gl.GL_MODELVIEW)

def OnDraw(self, *args, **kwargs):
self.pygletcontext.set_current()
gl.glClear(gl.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT)
self.thread.batch.draw()
self.canvas.SwapBuffers()


class wxPygletTest(wx.Frame):
'''wxPython & Pyglet.'''

def __init__(self, parent=None, id=wx.ID_ANY, *args, **kwargs):
super(wxPygletTest, self).__init__(parent, id, *args, **kwargs)

menuBar = wx.MenuBar()
mf = wx.Menu()
menuBar.Append(mf, "File")
item = wx.MenuItem(mf, wx.ID_ANY, "Exit")
self.Bind(wx.EVT_MENU, self.OnExit, item)
mf.AppendItem(item)
self.SetMenuBar(menuBar)

s = wx.BoxSizer(wx.HORIZONTAL)
self.panel = PygletPanel(self)
s.Add(self.panel, 1, wx.EXPAND, 5)
self.SetSizer(s)

print "testing"
self.Show()

def OnExit(self, event):
self.Destroy()

app = wx.App(redirect=False)
frame = wxPygletTest(size=(640,480), title="wxPyglet")
frame.Show()

app.MainLoop()
app.Destroy()

El 29/01/2012 6:42, Keith escribi�:

Keith

unread,
Mar 12, 2012, 2:12:10 PM3/12/12
to pyglet-users
Thanks for that example...I'll have to check it out!

--Keith Brafford

On Feb 11, 2:08 pm, Txema Vicente <tx...@nabla.net> wrote:
> Hi.
>
> I found this example in this list:http://groups.google.com/group/pyglet-users/browse_thread/thread/2308...

Ryexander

unread,
Mar 13, 2012, 1:00:32 PM3/13/12
to pyglet-users
HI, I'm the guy who made that original post in the list. I made
another post shortly after that I think that fixed a few things.

ya I found that post here:
https://groups.google.com/forum/?fromgroups#!searchin/pyglet-users/wx$20python/pyglet-users/gcNw3Xih3xM/0Tv7ZJ-XbD8J

in any case when initing the GL cnavas in wx you have the option of
using 32 or 24 bit color.I cant remember which but one of those modes
crashes on Linux and mac and the other does not.

good luck with what ever project your working on

Patel Hemangi

unread,
Jul 19, 2019, 10:03:48 AM7/19/19
to pyglet-users
hello , i have develop my application in pyglet now i want to merge wxpython gui with pyglet . so i have used your above code but i got this below error:

 if self.canvas.GetContext():
AttributeError: 'GLCanvas' object has no attribute 'GetContext'
main loop
Traceback (most recent call last):
  File "C:\Python_VS2015_pro\wxpython_glet\wxpython_glet\wxpython_glet.py", line 1058, in processPaintEvent
    self.canvas.SetCurrent()
TypeError: GLCanvas.SetCurrent(): not enough arguments

can anyone suggest me solution? 
Reply all
Reply to author
Forward
0 new messages