在mac:attribute上运行PyGTK错误:不支持xid属性



我尝试运行以下代码:

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()

我在wiki上安装了jhbuild教程中的GTK,我成功地运行了helloworld和其他一些教程。我对PyGame上的PyGTK包装器很感兴趣,我正在和几个人合作,其中一个人写了上面的代码来测试它。它运行得很好(他正在运行MintOS)。

在我的mac上我得到:

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

这个错误并不致命,我只是得到一个空白窗口。我想知道如何纠正这个错误。这在mac上可能吗?

从macport读取对gtk的响应,无论是否使用x11和osxvideosink:如何获取窗口id,听起来你的问题可能是因为你安装的PyGTK版本被编译为支持Quartz而不是x11。如果确实是这样,那么您可以尝试在OSX上将window.xid替换为window.nsview。即,更改:

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

类似的东西:

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

此外,如果window.nsview不起作用,您可以尝试window.nswindow

最新更新