Pygame event problem

Pete Shinners pete at shinners.org
Wed Sep 25 12:22:57 EDT 2002


Anand wrote:
>   I wrote a simple image viewer in python using pygame image api. The
> GUI is in wxPython. There are 2 classes, one for the window frame and one
> for the pygame image processing.

>    The effect is stopped by clicking with the mouse on the pygame
> display window. My problem is that if I try to invoke any other menu methods
> in the main GUI frame *without doing this*, the application ie both the
> wxFrame and the pygame display window, hangs.
> 
> 
>   I have a gut-feeling that this is because I am calling a method on
> the MyPyGameImgViewer class without exiting the while loop.
> 
>  Is there a way around this, ie to call any method in the
> MyPyGameImgViewer class from the GUI class, while one of its methods
> is executing a while loop

i'd be a little afraid of mixing wxpython and pygame like this, but it
sounds like you are pretty close to running. to exit the pygame loop you
could probably just pass it a mouse button event

e = pygame.event.Event(MOUSEBUTTONDOWN)
pygame.event.post(e)


of course, that also means you'll probably want to clear the pygame event
queue before starting your while loop. also, to clear the event queue it
will probably be better to get _all_ the events from the queue. otherwise
it will fill up with mouse motion and other types of events. change the
event part of the loop to this

if pygame.event.peek(MOUSEBUTTONDOWN):
   pygame.event.get()
   break

instead, you could also filter the pygame event queue, so only mouse button
events were put onto the queue. the code for that would be like this

pygame.event.set_allowed(None) #allow no events
pygame.event.set_allowed(MOUSEBUTTONDOWN) #only allow buttondown



also, there are ways to get SDL to work within an embedded window.
therefore embed into a wxpython control. basically it involves setting an
environment variable to an existing window id/handle. wxpython allows you
to get the id/handle you need. the code to embed the SDL window looks kind
of like this:


handle = wxpython_widget.get_window_id_i_forget_how()
os.environ['SDL_WINDOWID'] = str(handle)
if sys.platform == 'win32':
    os.environ['SDL_VIDEODRIVER'] = 'windib' #no directx embedded
pygame.init()
size_is_ignored = 10, 10
window = pygame.display.set_mode(size_is_ignored)

also, since you are likely only using the display portion of pygame, no
need to initialize the whole thing (like audio/cd/joystick/etc). you could
change the "pygame.init()" call do just "pygame.display.init()"

that should be pretty slick when you get it working. although it's been a
long time since i did anything like this. this should be all the correct info.

oh, also remember, when you embed the SDL window like this, it will not
receive any input events. you'll need to use wxpython's event handling for
that.





More information about the Python-list mailing list