Question

I tried running the following code:

import os, sys
import gtk
import pygame
from pygame.locals import *
import gobject

class GameWindow(gtk.Window):
     def __init__(self):
         gtk.Window.__init__(self)
         self.set_title("TEST")
         self.connect('delete-event', gtk.main_quit)
         self.set_resizable(True)

     def draw(self):
         #menu stuff here
         mb = gtk.MenuBar()
         menu1 = gtk.Menu()

         #anything in 'File' starts here
         menu1sub1 = gtk.MenuItem("File")
         menu1sub1.set_submenu(menu1)
         exit = gtk.MenuItem("Exit")
         exit.connect("activate", gtk.main_quit)
         menu1.append(exit)
         mb.append(menu1sub1)

         #drawing area to hold pygame
         da = gtk.DrawingArea()
         da.set_app_paintable(True)
         da.set_size_request(550,450)
         da.connect("realize",self._realized)

         #container that holds menu and drawing area
         #gtk.Window can only hold one widget
         #this always it to hold more
         vbox = gtk.VBox(False, 2)
         #packs menu then below that it will pack the drawing area
         vbox.pack_start(mb)
         vbox.pack_start(da)
         self.add(vbox)


         gtk.gdk.flush()
         self.show_all()

     def _realized(self, widget, data=None):
         #an id is needed for the connection between da and pygame
         os.putenv('SDL_WINDOWID', str(widget.window.xid))
         pygame.init()
         pygame.display.set_mode((550,450), 0, 32)
         self.screen = pygame.display.get_surface()
         #every 200 milliseconds gobject calls game
         gobject.timeout_add(200, self.game)

     #this is code to initialize the game
     def game(self):
         self.cup = [pygame.image.load("cup.png"),pygame.image.load("cup.png"),pygame.image.load("cup.png"),pygame.image.load("cup.png")]
         self.cupwidth = pygame.Surface.get_width(self.cup[0])
         self.cupheight = pygame.Surface.get_height(self.cup[0])
         self.cupx=[0,0,0,0]
         self.cupy=[450-self.cupheight,450-self.cupheight-25,450-self.cupheight-50,450-self.cupheight-75]
         self.screen.fill((0,0,0))
         self.screen.blit(self.cup[0],(self.cupx[0],self.cupy[0]))
         pygame.display.update()

if __name__ == "__main__":
     wi = GameWindow()
     wi.draw()
     gtk.main()

I installed GTK from jhbuild tutorial on the wiki, I successfully ran helloworld and a couple of others. I am interested in having PyGTK wrapper over PyGame, I'm working with a couple of people, and one of them wrote the above code to test it. It runs fine apparently (he's running MintOS).

On my mac I get:

me:Downloads Me$ python gamewindow.py
Traceback (most recent call last):
  File "gamewindow.py", line 48, in _realized
    os.putenv('SDL_WINDOWID', str(widget.window.xid))
AttributeError: xid attribute not supported

^CTraceback (most recent call last):
  File "gamewindow.py", line 69, in <module>
    gtk.main()
KeyboardInterrupt

The error isn't fatal, I just get a blank window. and I'm wondering how to correct the error. Is this possible on a mac?

Was it helpful?

Solution

Reading the responses to gtk from macport with or without x11 and osxvideosink: how to get the window id, it sounds like your problem might be because your installed version of PyGTK is compiled to support Quartz instead of X11. If that is indeed the case, then you could try replacing window.xid with window.nsview on OSX. I.e., change:

os.putenv('SDL_WINDOWID', str(widget.window.xid))

To something like this:

if sys.platform == "darwin":
    window_id = widget.window.nsview
else:
    window_id = widget.window.xid
os.putenv('SDL_WINDOWID', str(window_id))

Also, if window.nsview does not work you could try window.nswindow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top