问题
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.
回答1:
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.
回答2:
You may be interested in this message thread. Looks like they recommend against it.
回答3:
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.
回答4:
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)
回答5:
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.
回答6:
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.
回答7:
The Sugar project has several Activities built with PyGTK and PyGame.
They wrote a support lib to achieve this, called Sugargame. You should be able to modify it for regular PyGTK apps instead of Sugar.
Here's a chapter in Sugar's development book about how to use it.
The lib allows for communicating events between GTK and PyGame.
Enjoy!
来源:https://stackoverflow.com/questions/25661/pygame-within-a-pygtk-application