normal python widgets and pyglet

440 views
Skip to first unread message

simpsus_science

unread,
Feb 1, 2008, 5:32:52 AM2/1/08
to pyglet-users
Hallo,

i would like to code a little application that features a pyglet area
in the centre and some menus at the top as well as some buttons and
other widgets on the side of the window.
My question therefore is:
is there a way to combine a pyglet window with a standard one (like
the one i can create using eg glade).
ideally i would like to be able to include the pyglet window as a
widget inside a normal python window to get to stuff like menu bar and
gtk borders and so on for free.

is there anyone who can help me?

thanks a lot in advance!

Bastian

Gary Herron

unread,
Feb 1, 2008, 4:21:55 PM2/1/08
to pyglet-users
It *is* possible to use some other system to open an OpenGL window and
provide the whole GUI experience (menus, dialogs, toolbars, and event
loop), but use the OpenGL portions of pyglet to manipulate the OpenGL
window. This could work with many GUI toolkits -- GLUT, GTK+,
FLTK, ...
. This however, is quite far from what you are asking.

If what you want is pyglet opening the window and providing the event
loop, and then some other system providing the widgets/menus/
dialogs/...
on that window, then I too would like to see if someone else can
provide an answer.

Lunpa, The

unread,
Feb 2, 2008, 3:20:39 PM2/2/08
to pyglet...@googlegroups.com
Some toolkits can provide an opengl widget. Gtk does have one, but it
is an add on, so it would require an extra dependancy.

Gui toolkits provide something called sockets and plugs; if you can
get the window id of a pyglet window, you could have a socket widget
"swallow" the window (thus drawing turning the pyglet window into a
gtk widget). Gtk and QT can both do this, though this probably
introduces some portability problems; I do not know if this works in
osx or windows.

A more important problem in combining toolkits is processing events;
most toolkits have an event loop that blocks, but you often can call
one or two methods that manually check for updates; alternatively,
you can probably use a "time out" event to call your rendering methods
for opengl from the toolkit.

The problem with either scenario is timing; if you use the normal
timing method for one, the other doesn't behave quite like you'd
normally expect.


Its a huge hack either way. The best thing to use would be a toolkit
thats drawn via opengl; pyglet can spawn multiple windows, so it
would be a pretty nice for that.

But; I don't know of any such thing so far? (I'm working on such a
thing for the reasons above, but its not anywhere near complete enough
to be usefull)

--
This email message is public domain. Have a nice day! ^_^

Peter Wang

unread,
Feb 25, 2008, 12:34:45 PM2/25/08
to pyglet...@googlegroups.com
Hey guys, I just saw this thread.

Last week I was able to get some basic integration between wxPython
and pyglet - some sample code is attached below. Although this
program runs just fine, my actual usage is more complex and I've
encountered some issues. Namely:

1. sometimes calling set_current() on both the pyglet Context and on
the wx.GLCanvas results in a GL "invalid operation" exception
(primarily on win32, not on OS X); however, I seem to need to call
both in order to get correct rendering. (For example, pyglet's text
rendering won't work until set_current has been called.)

2. sometimes the window is blank until I resize, regardless of how
many times I swap buffers (via pyglet or wx), or how many times I draw
into the window or interact with the GL area with my mouse.

If anyone has any suggestions or ideas on how best to fix/debug these
issues, I'd love to hear from them.


Thanks,
Peter

-----------------------------

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

from pyglet import window, gl
from pyglet.font import load, Text

class myGLCanvas(GLCanvas):
def __init__(self, parent):
GLCanvas.__init__(self, parent,-1)
EVT_PAINT(self, self.OnPaint)
self.init = 0
return

def OnPaint(self,event):
if not self.init:
self.InitGL()
self.init = 1
self.ctx.set_current()
self.SetCurrent()
self.OnDraw()
return

def InitGL(self):
self.init_pyglet()
size = self.GetSize()
glViewport(0, 0, size.width, size.height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, size.width, 0, size.height, 1, -1);
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

def OnDraw(self):
# Clear the background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
size = self.GetSize()
glColor(0.827, 0.827, 0.827, 1.0)
glRectf(0, 0, size.width, size.height)

# draw a box
glColor(1, 0, 0, 1)
glLineWidth(2)
glBegin(GL_POLYGON)
glVertex2f(50, 50)
glVertex2f(50, 100)
glVertex2f(100, 100)
glVertex2f(100, 50)
glEnd()

# Draw the pyglet text
self.text.draw()

self.SwapBuffers()

def init_pyglet(self):
self.ctx = gl.Context()
self.ctx.set_current()
ft = load("Comic Sans MS", 24)
self.text = Text(ft, "Pyglet FTW!", x=150, y=50, color=(0,0,0,1))

def main():
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,'wxPyglet',wx.DefaultPosition,wx.Size(400,400))
canvas = myGLCanvas(frame)
frame.Show()
app.MainLoop()

if __name__ == '__main__': main()

Drew Smathers

unread,
Feb 25, 2008, 1:03:20 PM2/25/08
to pyglet...@googlegroups.com

Hey Peter,

Interesting. Pyglet and GUI integration would definitely be cool.
Maybe I missed something in your code, but why are you importing from
OpenGL instead of pyglet.gl? pyglet provides a ctypes binding around
OpenGL, so ideally your application should depend on pyglet or
pyOpenGL - but not both.

Cheers,
-Drew

Robert Kern

unread,
Feb 25, 2008, 1:10:20 PM2/25/08
to pyglet...@googlegroups.com
On Mon, Feb 25, 2008 at 12:03 PM, Drew Smathers <drew.s...@gmail.com> wrote:

> Hey Peter,
>
> Interesting. Pyglet and GUI integration would definitely be cool.
> Maybe I missed something in your code, but why are you importing from
> OpenGL instead of pyglet.gl? pyglet provides a ctypes binding around
> OpenGL, so ideally your application should depend on pyglet or
> pyOpenGL - but not both.

We use numpy extensively and PyOpenGL's array support is useful. This
example doesn't need it, of course.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco

Peter Wang

unread,
Feb 25, 2008, 3:43:49 PM2/25/08
to pyglet...@googlegroups.com
Yeah, I am remiss in not cleaning all the cruft out of this example
before posting it. (This was originally a wxPython + PyOpenGL
tutorial/example.)


-Peter

Richard Jones

unread,
Feb 25, 2008, 3:46:42 PM2/25/08
to pyglet...@googlegroups.com
On Tue, 26 Feb 2008, Robert Kern wrote:
> On Mon, Feb 25, 2008 at 12:03 PM, Drew Smathers <drew.s...@gmail.com>
wrote:
> > Hey Peter,
> >
> > Interesting. Pyglet and GUI integration would definitely be cool.
> > Maybe I missed something in your code, but why are you importing from
> > OpenGL instead of pyglet.gl? pyglet provides a ctypes binding around
> > OpenGL, so ideally your application should depend on pyglet or
> > pyOpenGL - but not both.
>
> We use numpy extensively and PyOpenGL's array support is useful. This
> example doesn't need it, of course.

Also PyOpenGL's layer is a little nicer to use - pyglet's is intentionally
raw.


Richard

Alec Thomas

unread,
Feb 25, 2008, 6:37:22 PM2/25/08
to pyglet...@googlegroups.com
On 26/02/2008, Richard Jones <r1char...@gmail.com> wrote:
> Also PyOpenGL's layer is a little nicer to use - pyglet's is intentionally
> raw.

Why intentionally?

--
Evolution: Taking care of those too stupid to take care of themselves.

Richard Jones

unread,
Feb 25, 2008, 6:39:48 PM2/25/08
to pyglet...@googlegroups.com
On Tue, 26 Feb 2008, Alec Thomas wrote:
> On 26/02/2008, Richard Jones <r1char...@gmail.com> wrote:
> > Also PyOpenGL's layer is a little nicer to use - pyglet's is
> > intentionally raw.
>
> Why intentionally?

It's a little faster if you don't try to do all the nice pythonic PyOpenGL
stuff :)


Richard

Matthew Marshall

unread,
Feb 26, 2008, 12:09:04 PM2/26/08
to pyglet...@googlegroups.com
On Monday 25 February 2008 14:34:45 Peter Wang wrote:
> 1. sometimes calling set_current() on both the pyglet Context and on
> the wx.GLCanvas results in a GL "invalid operation" exception
> (primarily on win32, not on OS X); however, I seem to need to call
> both in order to get correct rendering. (For example, pyglet's text
> rendering won't work until set_current has been called.)

There's a good chance that the GL error happened *before* set_current was
called. It's just that set_current found the error and raised an exception.

Try calling glGetError() first.

> 2. sometimes the window is blank until I resize, regardless of how
> many times I swap buffers (via pyglet or wx), or how many times I draw
> into the window or interact with the GL area with my mouse.

Maybe the viewport or projection matrix hasn't been set? Try setting them
every frame.

MWM

Reply all
Reply to author
Forward
0 new messages