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

<p>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.</p>

[{'answer_id': 25761, 'author': 'tslocum', 'author_id': 1662, 'author_profile': 'https://Stackoverflow.com/users/1662', 'pm_score': 1, 'selected': False, 'text': '<p>You may be interested in <a href="http://www.daa.com.au/pipermail/pygtk/2006-September/012888.html" rel="nofollow noreferrer">this message thread</a>.  Looks like they recommend against it.</p>\n'}, {'answer_id': 28935, 'author': 'dwestbrook', 'author_id': 3119, 'author_profile': 'https://Stackoverflow.com/users/3119', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>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: <a href="http://www.pygame.org/project/108/" rel="nofollow noreferrer">PGU</a> and <a href="http://www.pygame.org/project/125/" rel="nofollow noreferrer">OcempGUI</a>. The full list on the pygame site is <a href="http://www.pygame.org/tags/gui" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 31950, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 1, 'selected': False, 'text': '<p>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. </p>\n\n<p><a href="http://faq.pygtk.org/index.py?req=show&amp;file=faq23.042.htp" rel="nofollow noreferrer">This page</a> may help, though, if you want to try it.</p>\n'}, {'answer_id': 88457, 'author': 'Jez', 'author_id': 15478, 'author_profile': 'https://Stackoverflow.com/users/15478', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 199288, 'author': 'CyberFonic', 'author_id': 23999, 'author_profile': 'https://Stackoverflow.com/users/23999', 'pm_score': 1, 'selected': False, 'text': "<p>There's a simple solution that might work for you.  </p>\n\n<p>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.</p>\n"}, {'answer_id': 341525, 'author': 'Johan Dahlin', 'author_id': 14337, 'author_profile': 'https://Stackoverflow.com/users/14337', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://faq.pygtk.org/index.py?file=faq23.042.htp&amp;req=show" rel="nofollow noreferrer">http://faq.pygtk.org/index.py?file=faq23.042.htp&amp;req=show</a> mentions it all:</p>\n\n<p>You need to create a drawing area and set the environment variable SDL_WINDOWID after it\'s realized:</p>\n\n<pre>\n import os\n\n import gobject\n import gtk\n import pygame\n\n WINX = 400\n WINY = 200\n\n window = gtk.Window()\n window.connect(\'delete-event\', gtk.main_quit)\n window.set_resizable(False)\n area = gtk.DrawingArea()\n area.set_app_paintable(True)\n area.set_size_request(WINX, WINY)\n window.add(area)\n area.realize()\n\n # Force SDL to write on our drawing area\n os.putenv(\'SDL_WINDOWID\', str(area.window.xid))\n\n # We need to flush the XLib event loop otherwise we can\'t\n # access the XWindow which set_mode() requires\n gtk.gdk.flush()\n\n pygame.init()\n pygame.display.set_mode((WINX, WINY), 0, 0)\n screen = pygame.display.get_surface()\n\n image_surface = pygame.image.load(\'foo.png\')\n screen.blit(image_surface, (0, 0))\n\n gobject.idle_add(pygame.display.update)\n\n window.show_all()\n\n while gtk.event_pending():\n     # pygame/SDL event processing goes here\n     gtk.main_iteration(False)\n</pre>\n'}, {'answer_id': 19860670, 'author': 'icarito', 'author_id': 1112124, 'author_profile': 'https://Stackoverflow.com/users/1112124', 'pm_score': 0, 'selected': False, 'text': '<p>The Sugar project has several Activities built with PyGTK and PyGame.</p>\n\n<p>They wrote a support lib to achieve this, called <a href="http://wiki.sugarlabs.org/go/Development_Team/Sugargame" rel="nofollow">Sugargame</a>. You should be able to modify it for regular PyGTK apps instead of Sugar.</p>\n\n<p>Here\'s a <a href="http://georgejhunt.com/olpc/pydebug/ActivityBook/ActivitiesGuideSugar__ActivitiesUsingPyGame.html" rel="nofollow">chapter in Sugar\'s development book</a> about how to use it.</p>\n\n<p>The lib allows for communicating events between GTK and PyGame.</p>\n\n<p>Enjoy!</p>\n'}]
['https://Stackoverflow.com/questions/25661', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2772/']