views:

1263

answers:

7

What is the best way to use PyGame (SDL) within a PyGTK application?

I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.

A: 

You may be interested in this message thread. Looks like they recommend against it.

tj9991
+5  A: 

I've never attempted it myself, but hearing plenty about other people who've tried, it's not a road you want to go down.

There is the alternative of putting the gui in pygame itself. There are plenty of gui toolkits built specifically for pygame that you could use. Most of them are rather unfinished, but there are 2 big, actively maintained ones: PGU and OcempGUI. The full list on the pygame site is here.

dwestbrook
A: 

PyGame works much better when it can manage its own window, or even better, use the whole screen. GTK has flexible enough widgets to allow creation of a drawing area.

This page may help, though, if you want to try it.

John Douthat
A: 

I tried doing this myself a while ago, and I never got it to work perfectly. Actually I never got it to work at all under Windows, as it kept crashing the entire OS and I ran out of patience. I continued to use it though as it was only important it ran on Linux, and was only a small project. I'd strongly recommend you investigate alternatives. It always felt like a nasty hack, and made me feel dirty.

Jez
A: 

There's a simple solution that might work for you.

Write the PyGTK stuff and PyGame stuff as separate applications. Then from the PyGTK application call the PyGame application, using os.system to call the PyGame application. If you need to share data between the two then either use a database, pipes or IPC.

CyberED
+1  A: 

http://faq.pygtk.org/index.py?file=faq23.042.htp&req=show mentions it all:

You need to create a drawing area and set the environment variable SDL_WINDOWID after it's realized:

 import os

 import gobject
 import gtk
 import pygame

 WINX = 400
 WINY = 200

 window = gtk.Window()
 window.connect('delete-event', gtk.main_quit)
 window.set_resizable(False)
 area = gtk.DrawingArea()
 area.set_app_paintable(True)
 area.set_size_request(WINX, WINY)
 window.add(area)
 area.realize()

 # Force SDL to write on our drawing area
 os.putenv('SDL_WINDOWID', str(area.window.xid))

 # We need to flush the XLib event loop otherwise we can't
 # access the XWindow which set_mode() requires
 gtk.gdk.flush()

 pygame.init()
 pygame.display.set_mode((WINX, WINY), 0, 0)
 screen = pygame.display.get_surface()

 image_surface = pygame.image.load('foo.png')
 screen.blit(image_surface, (0, 0))

 gobject.idle_add(pygame.display.update)

 window.show_all()

 while gtk.event_pending():
     # pygame/SDL event processing goes here
     gtk.main_iteration(False)
Johan Dahlin
A: 

tanks for the the code i've tried this for a wek but no results...tanks!!!