pyGame

Is it possible to use the pygame module with wxpython?

···

_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* http://join.msn.com/?page=features/virus

Alejandro Adecoar wrote:

Is it possible to use the pygame module with wxpython?

Yes, but I don't know any details. I've only heard about people doing it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

This question comes up a lot. Attached is some sample code shamelessly
cribbed from the list archive
(ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.) and cleaned
(only slightly). I've also created a page in the wiki where I'll put
this information. You can find it at
http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
additional information, please put it there so we can have a centralized
body of knowledge on this popular topic.

Nathan

wxpyg.py (2.65 KB)

···

On Sun, 2003-05-04 at 20:18, Alejandro Adecoar wrote:

Is it possible to use the pygame module with wxpython?

_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

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

--
Nathan R. Yergler
http://yergler.net/~nathan

Nathan R. Yergler wrote:

I've also created a page in the wiki where I'll put
this information. You can find it at
http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
additional information, please put it there so we can have a centralized
body of knowledge on this popular topic.

Thanks for doing this! The more people that help out with the wiki the better it will get.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

This sample doesn't actually work for me. Perhaps the problem is Numeric, in
which case either a specific version of Numeric needs to be specified or we
need a much simpler example that doesn't require Numeric.

C:\mypython\mp3>gradientTest.py
Traceback (most recent call last):
  File "C:\mypython\mp3\gradientTest.py", line 38, in OnIdle
    self.draw()
  File "C:\mypython\mp3\gradientTest.py", line 79, in draw
    column = surfarray.map_array(surface, column)
ValueError: unsupported bytesperpixel for array

I'm running Windows 2000 (SP3), Python 2.2.2, wxPython 2.4.0.7, PyGame 1.5.5
(binary install), Numeric 22.0. Commenting out the draw lines allows the
code to run.

Also, at least on Windows, the PyGame window does not appear in the wxFrame,
which I think is the intent. I'm pretty sure that long ago, maybe wxPython
2.3.2 we were able to get the PyGame display to appear in a wxWindow and the
primary stumbling block is that now that doesn't seem to work. Does it work
correctly on Mac OS X or Linux?

Another problem is that if you close the PyGame window it will cause the
Python interpreter to crash.

I would be really happy if Robin and Pete could get wxPython and PyGame
co-operating again since then we would be able to play MPEGs and do some
other display magic in wxPython cross-platform rather than just using COM on
Windows for multimedia.

ka

···

-----Original Message-----
From: Nathan R. Yergler

This question comes up a lot. Attached is some sample code shamelessly
cribbed from the list archive
(ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.) and cleaned
(only slightly). I've also created a page in the wiki where I'll put
this information. You can find it at
http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
additional information, please put it there so we can have a centralized
body of knowledge on this popular topic.

Nathan

On Sun, 2003-05-04 at 20:18, Alejandro Adecoar wrote:
> Is it possible to use the pygame module with wxpython?
--
Nathan R. Yergler
http://yergler.net/~nathan

This sample I built works on Win2k too.
I'm using the latest stable versions of both pyGame and wxWindows.
I've been able to track down the problem of SDL surfaces popping up in
another window delaying the "import pygame" statement. It seems that on
win2k just importing pygame causes some initialization, preventing the user
to change some parameter like the windowid.
Hope that this can be fixed because the code looks really ugly.
I could not try it on Linux or other platforms therefore I don't know if it
works at all.

Riccardo

from wxPython.wx import *
import os
import thread
pyGame=None

class SDLThread:
def __init__(self,screen):
  self.m_bKeepGoing=self.m_bRunning=False
  self.screen=screen
  self.color=(255,0,0)
  self.rect=(10,10,100,100)
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):
  while self.m_bKeepGoing:
   e=pyGame.event.poll()
   if e.type==pyGame.MOUSEBUTTONDOWN:
    self.color=(255,0,128)
    self.rect=(e.pos[0],e.pos[1],100,100)
    print e.pos
   self.screen.fill((0,0,0))
   self.screen.fill(self.color,self.rect)
   pyGame.display.flip()
  self.m_bRunning=False;

class SDLPanel(wxPanel):
def __init__(self,parent,ID,tplSize):
  global pyGame
  wxPanel.__init__(self,parent,ID,size=tplSize)
  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(tplSize)
  self.thread=SDLThread(window)
  self.thread.Start()

class MyFrame(wxFrame):
def __init__(self,parent,ID,strTitle,tplSize):
  wxFrame.__init__(self,parent,ID,strTitle,size=tplSize)
  self.pnlSDL=SDLPanel(self,-1,tplSize)
  #self.Fit()
app = wxPySimpleApp()
frame = MyFrame(NULL, -1, "SDL Frame",(640,480))
frame.Show()
app.MainLoop()

···

----- Original Message -----
From: "Kevin Altis" <altis@semi-retired.com>
To: <wxPython-users@lists.wxwindows.org>
Sent: Tuesday, May 06, 2003 23:25
Subject: RE: [wxPython-users] pyGame

> -----Original Message-----
> From: Nathan R. Yergler
>
> This question comes up a lot. Attached is some sample code shamelessly
> cribbed from the list archive
> (ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.) and cleaned
> (only slightly). I've also created a page in the wiki where I'll put
> this information. You can find it at
> http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
> additional information, please put it there so we can have a centralized
> body of knowledge on this popular topic.
>
> Nathan
>
>
> On Sun, 2003-05-04 at 20:18, Alejandro Adecoar wrote:
> > Is it possible to use the pygame module with wxpython?
> --
> Nathan R. Yergler
> http://yergler.net/~nathan

This sample doesn't actually work for me. Perhaps the problem is Numeric,

in

which case either a specific version of Numeric needs to be specified or

we

need a much simpler example that doesn't require Numeric.

C:\mypython\mp3>gradientTest.py
Traceback (most recent call last):
  File "C:\mypython\mp3\gradientTest.py", line 38, in OnIdle
    self.draw()
  File "C:\mypython\mp3\gradientTest.py", line 79, in draw
    column = surfarray.map_array(surface, column)
ValueError: unsupported bytesperpixel for array

I'm running Windows 2000 (SP3), Python 2.2.2, wxPython 2.4.0.7, PyGame

1.5.5

(binary install), Numeric 22.0. Commenting out the draw lines allows the
code to run.

Also, at least on Windows, the PyGame window does not appear in the

wxFrame,

which I think is the intent. I'm pretty sure that long ago, maybe wxPython
2.3.2 we were able to get the PyGame display to appear in a wxWindow and

the

primary stumbling block is that now that doesn't seem to work. Does it

work

correctly on Mac OS X or Linux?

Another problem is that if you close the PyGame window it will cause the
Python interpreter to crash.

I would be really happy if Robin and Pete could get wxPython and PyGame
co-operating again since then we would be able to play MPEGs and do some
other display magic in wxPython cross-platform rather than just using COM

on

Windows for multimedia.

ka

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

Kevin --

I updated the sample code in the wiki to remove the Numeric dependancy.
I'm still not really pleased with it, but don't have tons of time to
devote to the problem. If you could try it on Win32, I'd appreciate it.

Thanks,

Nathan

···

On Tue, 2003-05-06 at 16:25, Kevin Altis wrote:

> -----Original Message-----
> From: Nathan R. Yergler
>
> This question comes up a lot. Attached is some sample code shamelessly
> cribbed from the list archive
> (ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.) and cleaned
> (only slightly). I've also created a page in the wiki where I'll put
> this information. You can find it at
> http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
> additional information, please put it there so we can have a centralized
> body of knowledge on this popular topic.
>
> Nathan
>
>
> On Sun, 2003-05-04 at 20:18, Alejandro Adecoar wrote:
> > Is it possible to use the pygame module with wxpython?
> --
> Nathan R. Yergler
> http://yergler.net/~nathan

This sample doesn't actually work for me. Perhaps the problem is Numeric, in
which case either a specific version of Numeric needs to be specified or we
need a much simpler example that doesn't require Numeric.

C:\mypython\mp3>gradientTest.py
Traceback (most recent call last):
  File "C:\mypython\mp3\gradientTest.py", line 38, in OnIdle
    self.draw()
  File "C:\mypython\mp3\gradientTest.py", line 79, in draw
    column = surfarray.map_array(surface, column)
ValueError: unsupported bytesperpixel for array

I'm running Windows 2000 (SP3), Python 2.2.2, wxPython 2.4.0.7, PyGame 1.5.5
(binary install), Numeric 22.0. Commenting out the draw lines allows the
code to run.

Also, at least on Windows, the PyGame window does not appear in the wxFrame,
which I think is the intent. I'm pretty sure that long ago, maybe wxPython
2.3.2 we were able to get the PyGame display to appear in a wxWindow and the
primary stumbling block is that now that doesn't seem to work. Does it work
correctly on Mac OS X or Linux?

Another problem is that if you close the PyGame window it will cause the
Python interpreter to crash.

I would be really happy if Robin and Pete could get wxPython and PyGame
co-operating again since then we would be able to play MPEGs and do some
other display magic in wxPython cross-platform rather than just using COM on
Windows for multimedia.

ka

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

--
Nathan R. Yergler
http://yergler.net/~nathan

I tried the code you provided on Linux and ran into a couple issues.
First, the SDL_VIDEODRIVER environment variable should only be set to
windib if sys.platform == 'win32'. Even then there's an issue I haven't
figured out how to get around. If you set the environment variable
SDL_WINDOWID to the proper window handle, and then call pygame.init()
within the frame's __init__, you get a Gdk-ERROR that you have specified
a bad window handle. It seems that on Linux the window handle isn't
really there until the constructor finishes. I suppose you could move
the initialization code to an OnPaint handler, like the example
currently in the wiki. If I get a chance to do some refinement and get
the code to work on Linux I'll put it in the wiki as a second example.

NRY

···

On Wed, 2003-05-07 at 06:13, Riccardo Trocca wrote:

This sample I built works on Win2k too.
I'm using the latest stable versions of both pyGame and wxWindows.
I've been able to track down the problem of SDL surfaces popping up in
another window delaying the "import pygame" statement. It seems that on
win2k just importing pygame causes some initialization, preventing the user
to change some parameter like the windowid.
Hope that this can be fixed because the code looks really ugly.
I could not try it on Linux or other platforms therefore I don't know if it
works at all.

Riccardo

from wxPython.wx import *
import os
import thread
pyGame=None

class SDLThread:
def __init__(self,screen):
  self.m_bKeepGoing=self.m_bRunning=False
  self.screen=screen
  self.color=(255,0,0)
  self.rect=(10,10,100,100)
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):
  while self.m_bKeepGoing:
   e=pyGame.event.poll()
   if e.type==pyGame.MOUSEBUTTONDOWN:
    self.color=(255,0,128)
    self.rect=(e.pos[0],e.pos[1],100,100)
    print e.pos
   self.screen.fill((0,0,0))
   self.screen.fill(self.color,self.rect)
   pyGame.display.flip()
  self.m_bRunning=False;

class SDLPanel(wxPanel):
def __init__(self,parent,ID,tplSize):
  global pyGame
  wxPanel.__init__(self,parent,ID,size=tplSize)
  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(tplSize)
  self.thread=SDLThread(window)
  self.thread.Start()

class MyFrame(wxFrame):
def __init__(self,parent,ID,strTitle,tplSize):
  wxFrame.__init__(self,parent,ID,strTitle,size=tplSize)
  self.pnlSDL=SDLPanel(self,-1,tplSize)
  #self.Fit()
app = wxPySimpleApp()
frame = MyFrame(NULL, -1, "SDL Frame",(640,480))
frame.Show()
app.MainLoop()

----- Original Message -----
From: "Kevin Altis" <altis@semi-retired.com>
To: <wxPython-users@lists.wxwindows.org>
Sent: Tuesday, May 06, 2003 23:25
Subject: RE: [wxPython-users] pyGame

> > -----Original Message-----
> > From: Nathan R. Yergler
> >
> > This question comes up a lot. Attached is some sample code shamelessly
> > cribbed from the list archive
> > (ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.) and cleaned
> > (only slightly). I've also created a page in the wiki where I'll put
> > this information. You can find it at
> > http://wiki.wxpython.org/index.cgi/IntegratingPyGame. Anyone with
> > additional information, please put it there so we can have a centralized
> > body of knowledge on this popular topic.
> >
> > Nathan
> >
> >
> > On Sun, 2003-05-04 at 20:18, Alejandro Adecoar wrote:
> > > Is it possible to use the pygame module with wxpython?
> > --
> > Nathan R. Yergler
> > http://yergler.net/~nathan
>
> This sample doesn't actually work for me. Perhaps the problem is Numeric,
in
> which case either a specific version of Numeric needs to be specified or
we
> need a much simpler example that doesn't require Numeric.
>
> C:\mypython\mp3>gradientTest.py
> Traceback (most recent call last):
> File "C:\mypython\mp3\gradientTest.py", line 38, in OnIdle
> self.draw()
> File "C:\mypython\mp3\gradientTest.py", line 79, in draw
> column = surfarray.map_array(surface, column)
> ValueError: unsupported bytesperpixel for array
>
> I'm running Windows 2000 (SP3), Python 2.2.2, wxPython 2.4.0.7, PyGame
1.5.5
> (binary install), Numeric 22.0. Commenting out the draw lines allows the
> code to run.
>
> Also, at least on Windows, the PyGame window does not appear in the
wxFrame,
> which I think is the intent. I'm pretty sure that long ago, maybe wxPython
> 2.3.2 we were able to get the PyGame display to appear in a wxWindow and
the
> primary stumbling block is that now that doesn't seem to work. Does it
work
> correctly on Mac OS X or Linux?
>
> Another problem is that if you close the PyGame window it will cause the
> Python interpreter to crash.
>
> I would be really happy if Robin and Pete could get wxPython and PyGame
> co-operating again since then we would be able to play MPEGs and do some
> other display magic in wxPython cross-platform rather than just using COM
on
> Windows for multimedia.
>
> ka
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org
>

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

--
Nathan R. Yergler
http://yergler.net/~nathan

I updated the sample code in the wiki to remove the Numeric dependancy.
I'm still not really pleased with it, but don't have tons of time to
devote to the problem. If you could try it on Win32, I'd appreciate it.

I liked the Numeric dependancy, since this was one of the things I got
interested in PyGame :slight_smile: But maybe I'll add an example later, when I have it
the way I want it to be.

One of the things I'm not pleased with is the fact that it constantly
refreshes when moving the mouse over the window. This is quite annoying,
since it also gives a flicker effect.

What could I do to get rid of this?

bye,
Kasper

Hi all,

I just stumbled across something I imagine everyone *except* me was already
aware of....

I have an application where I was capturing key presses as menu hotkeys.
Well, I coded up all of this logic to hook any EVT_CHAR and EVT_KEY_UP
events to my multitude of child windows and pass them up to the parent
wxFrame. Then, in the parent I have this OnChar handler which decodes the
keycodes and fires methods according to the Ctrl-Key combination.

Well, it appears this is all useless code! I have recently changed my
menus to be "standard" in that the shortcut key appears after the menu item
label with a tab character and "Ctrl+" the keyname (i.e. "&Save\tCtrl+S"
for save - for example). I use to have them in brackets with a dash
instead of a plus sign. ANYWAY - I have been trying to figure out why my
program behaves differently today - and wouldn't you know it - wxPython
automatically hooks the keys now. So - I am getting TWO events instead of
one.

Here is my problem...I used to be relying on passing NULL as the event
parameter and bypassing certain annoying "Are you sure?" dialogs if the
user pressed the hotkey. Now I can't do that because the event passed is
always valid wxCommandEvent.

First question - Am I right in that wxPython automatically binds the
keyboard shortcuts (if you use the correct syntax)?

Second question - Am I really that much of an idiot and didn't read the
documentation clearly - or is this another one of those 'black art' tricks
known by experienced people?

Third question - Using the automatic key binding feature - what would be
the *correct* way to do what I am trying to do - which is tell whether or
not the user actually clicked the menu or pressed the hotkey?

Last question - Is it safe to go and rip out all of this crap I have
introduced to implement passing up EVT_CHAR events, and rely on the
auto-bound keys to be active all of the time, no matter what has the focus?

Thanks,
Mark.

Nathan R. Yergler wrote:

I tried the code you provided on Linux and ran into a couple issues. First, the SDL_VIDEODRIVER environment variable should only be set to
windib if sys.platform == 'win32'. Even then there's an issue I haven't
figured out how to get around. If you set the environment variable
SDL_WINDOWID to the proper window handle, and then call pygame.init()
within the frame's __init__, you get a Gdk-ERROR that you have specified
a bad window handle. It seems that on Linux the window handle isn't
really there until the constructor finishes. I suppose you could move
the initialization code to an OnPaint handler, like the example
currently in the wiki. If I get a chance to do some refinement and get
the code to work on Linux I'll put it in the wiki as a second example.

You are correct, the actual X-Window is not created in GTK until sometime after the wxWindows class is created. You can use EVT_WINDOW_CREATE to get an event when it has been created.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Nathan R. Yergler wrote:

[...] It seems that on Linux the window handle isn't
really there until the constructor finishes. I suppose you could move
the initialization code to an OnPaint handler, like the example
currently in the wiki.

I know nothing about PyGame, and don't have the time to really investigate ATM. However, if your problem is that you have initialization code that must run after the wxFrame.__init__() finishes, I'd recommend calling that code via wxCallAfter() from your frame's __init__(). This will let you avoid polluting your regular event handlers with initialization code.

Jeff Shannon
Technician/Programmer
Credit International

Jeff Shannon wrote:

Nathan R. Yergler wrote:

[...] It seems that on Linux the window handle isn't
really there until the constructor finishes. I suppose you could move
the initialization code to an OnPaint handler, like the example
currently in the wiki.

I know nothing about PyGame, and don't have the time to really investigate ATM. However, if your problem is that you have initialization code that must run after the wxFrame.__init__() finishes, I'd recommend calling that code via wxCallAfter() from your frame's __init__(). This will let you avoid polluting your regular event handlers with initialization code.

This is one cae where wxCallAfter happens too soon. EVT_WINDOW_CREATE is the right way to do it. Unfortunately you have to check the platform because on MSW and OSX the window is already created when you do the parentclass.__init__. For example, here is some code that will be in the demo in the next release:

class TestFrame(wxFrame):
     def __init__(self, parent, log):
         self.log = log
         wxFrame.__init__(self, parent, -1, "Shaped Window",
                          style = wxFRAME_SHAPED )

          ...

         if wxPlatform == "__WXGTK__":
             # wxGTK requires that the window be created before you can
             # set its shape, so delay the call to SetWindowShape until
             # this event.
             EVT_WINDOW_CREATE(self, self.SetWindowShape)
         else:
             # On wxMSW and wxMac the window has already been created,
             # so go for it.
             self.SetWindowShape()

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

One of the things I'm not pleased with is the fact that it constantly
refreshes when moving the mouse over the window. This is quite annoying,
since it also gives a flicker effect.

Yeah, I know that sucks. I think something like implementing a "dirty"
flag to control repainting would be the way to go. So I'm working on a
new sample that doesn't flicker, imports PyGame in the right spot and
listens for PyGame events. I'll put it on the Wiki when I get it done.

Nathan

···

--
Nathan R. Yergler
http://yergler.net/~nathan

Does PyGame support AVI files on Linux? If it does, does it work with the Windows codecs?

Bob

···

At 08:33 AM 5/8/2003 -0500, you wrote:

> One of the things I'm not pleased with is the fact that it constantly
> refreshes when moving the mouse over the window. This is quite annoying,
> since it also gives a flicker effect.

Yeah, I know that sucks. I think something like implementing a "dirty"
flag to control repainting would be the way to go. So I'm working on a
new sample that doesn't flicker, imports PyGame in the right spot and
listens for PyGame events. I'll put it on the Wiki when I get it done.