Integration of PyGame questions

Hi all

I have managed to integrate a PyGame panel into a wxPython frame as described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame

But now, I want to add wx widgets to the frame. Unofrtunately, this does not work as expected. It seems to be impossible to add the PygGame panel into sizers and create a nice interface around it. Imagine a few buttons above or below the panel. How could this be done?

My first try was to add a wx.Panel behind the PyGame panel and this worked so far as I could have wx wodgets below, but the background was transparent!!! It is also possible to add a toolbar, but it is very small. And why does the frame not resize to include the full size of the PyGame panel (frame size is equal to panel size, so the panel is truncated).

Another problem is that there can only be one PyGame panel. This is a PyGame problem, but if somebody happens to know how to run multiple PyGame screens I am very interested, of course.

Any help is very much appreciated!
thanks
André

PS Here is the working code so far:

import wx
import os
import thread

pyGame = None

class SDLThread(object):
def init(self, screen):

    self.m_bKeepGoing = self.m_bRunning = False
    self.screen = screen
    self.surface = pyGame.display.get_surface()
    self.rect = self.surface.get_rect()

def Start(self):
    self.m_bKeepGoing = self.m_bRunning = True
    thread.start_new_thread(self.Run, ())

def Stop(self):
    self.m_bKeepGoing = False

def IsRunning(self):
    return self.m_bRunning

def Run(self):
    self.setup()
    while self.m_bKeepGoing:
        if pyGame:
            self.update()
    self.m_bRunning = False;

def setup(self):
    self.color = (255, 0, 0)
    self.r = (10, 10, 100, 100)

def update(self):
    e = pyGame.event.poll()
    if e.type == pyGame.MOUSEBUTTONDOWN:
        self.color = (255, 0, 128)
        self.r = (e.pos[0], e.pos[1], 100, 100)
        print e.pos
    self.screen.fill((0, 0, 0))
    self.screen.fill(self.color, self.r)
    pyGame.draw.aaline(self.screen, (63, 191, 128), (0, 0), (

self.rect.width-1, self.rect.height-1))
pyGame.draw.aaline(self.screen, (63, 191, 128), (self.rect.width-1, 0), (0, self.rect.height-1))
pyGame.display.flip()

class SDLPanel(wx.Panel):
def init(self, parent, ID, game_size):

    global pyGame
    wx.Panel.__init__(self, parent, ID, size=game_size)
    self.Fit()
    os.environ['SDL_WINDOWID'] = str(self.GetHandle())
    os.environ['SDL_VIDEODRIVER'] = 'windib'

    import pygame
    pyGame = pygame
    pyGame.display.init()
    window = pyGame.display.set_mode(game_size)
    self.thread = SDLThread(window)
    self.thread.Start()

class MyFrame(wx.Frame):
def init(self, parent, ID, title, game_size):
width, height = game_size
wx.Frame.init(self, parent, ID, title, size=(width+39, height+34))
self.pnlSDL
= SDLPanel(self, -1, game_size)

outerbox = wx.BoxSizer(wx.VERTICAL)

self.SetSizer(outerbox)

···

width, height = game_size

gamepanel = wx.Panel(self, 998, pos=wx.Point(0, 0), size=

wx.Size(width, height))

outerbox.Add(gamepanel)

wxbox = wx.BoxSizer(wx.HORIZONTAL)

outerbox.Add(wxbox)

p = wx.Panel(self, 1000, pos=wx.Point(0, 0))

outerbox.Add

(wx.Panel(self, 1000, pos=wx.Point(0, 0)))

wxbox.Add(wx.Button(self, 1001, “Start”), flag=wx.ALL, border=3)

wxbox.Add(wx.Button(self, 1002, “Stop”), flag=wx.ALL, border=3)

self.SetAutoLayout(True)

self.Fit()

    toolbar = self.CreateToolBar(style=wx.TB_VERTICAL | wx.TB_TEXT | wx.NO_BORDER | wx.TB_FLAT)
   
    id_start = 2001
    toolbar.AddLabelTool

(id_start, label=‘Start’, bitmap=wx.Bitmap(‘start.png’), shortHelp=‘Start Simulation’)
wx.EVT_TOOL(self, id_start, self.OnStartSimulation)

    id_stop = 2002
    toolbar.AddLabelTool(id_stop, label='Stop', bitmap=

wx.Bitmap(‘stop.png’), shortHelp=‘Stop Simulation’)
wx.EVT_TOOL(self, id_stop, self.OnStopSimulation)

    id_quit = 2003
    toolbar.AddLabelTool(id_quit, label='Quit', bitmap=wx.Bitmap

(‘quit.png’), shortHelp=‘Quit Simulation’)
wx.EVT_TOOL(self, id_quit, self.OnCloseWindow)

    toolbar.Realize()
   
    self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

def OnStartSimulation(self, event):

    """Start button pressed"""
    print 'Start', event
   
def OnStopSimulation(self, event):
    """Stop button pressed"""

    print 'Stop', event
   
def OnSize(self, event):
    size = self.GetClientSize()
    print 'resizing', size
    if getattr(self, 'app', None):
        self.app.update

()
event.Skip()

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

app = wx.PySimpleApp()
frame = MyFrame(None, -1, “Danger Island”, (800,600))
frame.Show()
app.MainLoop
()

Hi

Hi all

I have managed to integrate a PyGame panel into a wxPython frame as
described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame

Well that's funny - today in the morning I had a similar idea. My goal
wasn't pygame but pyVideo with wx. pyVideo has a method toSDLSurface
that I use now.

But now, I want to add wx widgets to the frame. Unofrtunately, this does not
work as expected. It seems to be impossible to add the PygGame panel into
sizers and create a nice interface around it. Imagine a few buttons above or
below the panel. How could this be done?

Attached is a truncated version of my app. It shows how to use sizers
with a pygame-surface. Please note that it needs pyVideo and therefore
works only with python 2.3. But maybe you can take some ideas from it.

My first try was to add a wx.Panel behind the PyGame panel and this worked
so far as I could have wx wodgets below, but the background was
transparent!!! It is also possible to add a toolbar, but it is very small.
And why does the frame not resize to include the full size of the PyGame
panel (frame size is equal to panel size, so the panel is truncated).

I had the problem, that the size of the surface-panel didn't change,
when the size of surface changed. To solve this I had to use
surface-panel.SetMinSize instead of ....SetSize - in my app the method
camResize does this. But I wasn't able to figure out how to set the
whole frame to the correct size.

Another problem is that there can only be one PyGame panel. This is a PyGame
problem, but if somebody happens to know how to run multiple PyGame screens
I am very interested, of course.

Hmm I can start my app 2 times, select different video sources
and it works. But 2 surfaces in one app? I don't know.

cu boesi

integrate_pygame.py (5.35 KB)

···

Am 29.06.2006 09:47:16 schrieb Andre Meyer:
--
Ein Wunder muss heute schon ganz schoen
wundervoll sein um ein Wunder zu sein,
sonst wuerde man sich ja gar nicht mehr wundern
                 .-==Prof. Dr. Harald Lesch==-.

Hi boesi

Thanks a lot for the code, I’ll have a look at it tomorrow.

cu
André

···

On 6/29/06, Alexander ‘boesi’ Bösecke < boesi.josi@gmx.net> wrote:

Hi

Am 29.06.2006 09:47:16 schrieb Andre Meyer:

Hi all

I have managed to integrate a PyGame panel into a wxPython frame as
described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame

Well that’s funny - today in the morning I had a similar idea. My goal
wasn’t pygame but pyVideo with wx. pyVideo has a method toSDLSurface
that I use now.

But now, I want to add wx widgets to the frame. Unofrtunately, this does not

work as expected. It seems to be impossible to add the PygGame panel into
sizers and create a nice interface around it. Imagine a few buttons above or
below the panel. How could this be done?

Attached is a truncated version of my app. It shows how to use sizers
with a pygame-surface. Please note that it needs pyVideo and therefore
works only with python 2.3. But maybe you can take some ideas from it.

My first try was to add a wx.Panel behind the PyGame panel and this worked
so far as I could have wx wodgets below, but the background was
transparent!!! It is also possible to add a toolbar, but it is very small.

And why does the frame not resize to include the full size of the PyGame
panel (frame size is equal to panel size, so the panel is truncated).

I had the problem, that the size of the surface-panel didn’t change,

when the size of surface changed. To solve this I had to use
surface-panel.SetMinSize instead of …SetSize - in my app the method
camResize does this. But I wasn’t able to figure out how to set the
whole frame to the correct size.

Another problem is that there can only be one PyGame panel. This is a PyGame
problem, but if somebody happens to know how to run multiple PyGame screens
I am very interested, of course.

Hmm I can start my app 2 times, select different video sources
and it works. But 2 surfaces in one app? I don’t know.

cu boesi

Ein Wunder muss heute schon ganz schoen
wundervoll sein um ein Wunder zu sein,

sonst wuerde man sich ja gar nicht mehr wundern
.-==Prof. Dr. Harald Lesch==-.


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Dr. Andre P. Meyer http://python.openspace.nl/meyer
TNO Defence, Security and Safety
http://www.tno.nl/

Delft Cooperation on Intelligent Systems http://www.decis.nl/

Ah, this is obviously some strange usage of the word ‘safe’ that I wasn’t previously aware of. - Douglas Adams

Dear Bösi

I finally played around with your code and managed to get it to run without the camera lib (wc). Still need to figure out why this works and mine did not…

Do you know why the top level window does not resize properly to fit all the panels?

kind regards
André

···

On 6/29/06, Alexander ‘boesi’ Bösecke boesi.josi@gmx.net wrote:

Hi

Am 29.06.2006 09:47:16 schrieb Andre Meyer:

Hi all

I have managed to integrate a PyGame panel into a wxPython frame as

described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame

Well that’s funny - today in the morning I had a similar idea. My goal

wasn’t pygame but pyVideo with wx. pyVideo has a method toSDLSurface
that I use now.

But now, I want to add wx widgets to the frame. Unofrtunately, this does not
work as expected. It seems to be impossible to add the PygGame panel into

sizers and create a nice interface around it. Imagine a few buttons above or
below the panel. How could this be done?

Attached is a truncated version of my app. It shows how to use sizers
with a pygame-surface. Please note that it needs pyVideo and therefore

works only with python 2.3. But maybe you can take some ideas from it.

My first try was to add a wx.Panel behind the PyGame panel and this worked
so far as I could have wx wodgets below, but the background was

transparent!!! It is also possible to add a toolbar, but it is very small.
And why does the frame not resize to include the full size of the PyGame
panel (frame size is equal to panel size, so the panel is truncated).

I had the problem, that the size of the surface-panel didn’t change,
when the size of surface changed. To solve this I had to use
surface-panel.SetMinSize instead of …SetSize - in my app the method
camResize does this. But I wasn’t able to figure out how to set the

whole frame to the correct size.

Another problem is that there can only be one PyGame panel. This is a PyGame
problem, but if somebody happens to know how to run multiple PyGame screens
I am very interested, of course.

Hmm I can start my app 2 times, select different video sources
and it works. But 2 surfaces in one app? I don’t know.

cu boesi

Ein Wunder muss heute schon ganz schoen
wundervoll sein um ein Wunder zu sein,

sonst wuerde man sich ja gar nicht mehr wundern
.-==Prof. Dr. Harald Lesch==-.


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Dr. Andre P. Meyer http://python.openspace.nl/meyer
TNO Defence, Security and Safety
http://www.tno.nl/

Delft Cooperation on Intelligent Systems http://www.decis.nl/

Ah, this is obviously some strange usage of the word ‘safe’ that I wasn’t previously aware of. - Douglas Adams

Hi Boesi ea.

After some fiddling around I managed to get your code and mine running together. Thanks a lot for your help!

Attached you can find the current experiment. AutoLayout works now, as well. Maybe it’s interesting for other people, too.

Comments are always welcome.

kind regards
André

wxgame.py (7.15 KB)

ball.gif

···

On 6/29/06, Alexander ‘boesi’ Bösecke < boesi.josi@gmx.net> wrote:

Hi

Am 29.06.2006 09:47:16 schrieb Andre Meyer:

Hi all

I have managed to integrate a PyGame panel into a wxPython frame as
described at http://wiki.wxpython.org/index.cgi/IntegratingPyGame

Well that’s funny - today in the morning I had a similar idea. My goal
wasn’t pygame but pyVideo with wx. pyVideo has a method toSDLSurface
that I use now.

But now, I want to add wx widgets to the frame. Unofrtunately, this does not

work as expected. It seems to be impossible to add the PygGame panel into
sizers and create a nice interface around it. Imagine a few buttons above or
below the panel. How could this be done?

Attached is a truncated version of my app. It shows how to use sizers
with a pygame-surface. Please note that it needs pyVideo and therefore
works only with python 2.3. But maybe you can take some ideas from it.

My first try was to add a wx.Panel behind the PyGame panel and this worked
so far as I could have wx wodgets below, but the background was
transparent!!! It is also possible to add a toolbar, but it is very small.

And why does the frame not resize to include the full size of the PyGame
panel (frame size is equal to panel size, so the panel is truncated).

I had the problem, that the size of the surface-panel didn’t change,

when the size of surface changed. To solve this I had to use
surface-panel.SetMinSize instead of …SetSize - in my app the method
camResize does this. But I wasn’t able to figure out how to set the
whole frame to the correct size.

Another problem is that there can only be one PyGame panel. This is a PyGame
problem, but if somebody happens to know how to run multiple PyGame screens
I am very interested, of course.

Hmm I can start my app 2 times, select different video sources
and it works. But 2 surfaces in one app? I don’t know.

cu boesi

Ein Wunder muss heute schon ganz schoen
wundervoll sein um ein Wunder zu sein,

sonst wuerde man sich ja gar nicht mehr wundern
.-==Prof. Dr. Harald Lesch==-.


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Dr. Andre P. Meyer http://python.openspace.nl/meyer
TNO Defence, Security and Safety
http://www.tno.nl/

Delft Cooperation on Intelligent Systems http://www.decis.nl/

Ah, this is obviously some strange usage of the word ‘safe’ that I wasn’t previously aware of. - Douglas Adams

Hi

Hi Boesi ea.

After some fiddling around I managed to get your code and mine running
together. Thanks a lot for your help!

Attached you can find the current experiment. AutoLayout works now, as well.
Maybe it's interesting for other people, too.

Comments are always welcome.

nice demo :slight_smile:

Did you test it on W2k with python 2.3.5 and pygame 1.6? The attached
code crashes with the following error. On XP or on W2k with python 2.4.2
and pygame 1.7.1 it works. Your code shows the same behaviour. I've send
already a mail to the pygame-list.

---snip---
Pygame Parachute Traceback:
  File "simple_pygame.py", line 15, in __init__
Fatal Python error: (pygame parachute) Segmentation Fault

abnormal program termination
---snap---

cu boesi

simple_pygame.py (600 Bytes)

···

Am 07.07.2006 00:35:00 schrieb Andre Meyer: