我有以下Python 2.7/PyGObject 3.0/PyGST 0.10模块:

from gi.repository import Gtk, Gdk, GdkPixbuf
import pango
import pygst
pygst.require('0.10')
import gst
import Trailcrest
import os, sys
import cairo
from math import pi

class Video:

    def __init__(self):

        def on_message(bus, message):
            if message.type == gst.MESSAGE_EOS:
                # End of Stream
                player.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, gst.SEEK_TYPE_SET, 5000000000, gst.SEEK_TYPE_NONE, 6000000000)
            elif message.type == gst.MESSAGE_ERROR:
                player.set_state(gst.STATE_NULL)
                (err, debug) = message.parse_error()
                print "Error: %s" % err, debug

        def on_sync_message(bus, message):
            if message.structure is None:
                return False
            if message.structure.get_name() == "prepare-xwindow-id":
                Gdk.threads_enter()
                print "Run before"
                Gdk.Display.get_default().sync()
                print "Run after"
                win_id = videowidget.window.xid
                imagesink = message.src
                imagesink.set_property("force-aspect-ratio", True)
                imagesink.set_xwindow_id(win_id)
                Gtk.gdk.threads_leave()

        def click_me(event, data=None):
            player.seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, gst.SEEK_TYPE_SET, 5000000000, gst.SEEK_TYPE_NONE, 6000000000)

        win = Gtk.Window()
        win.set_resizable(False)
        win.set_decorated(False)
        win.set_position(Gtk.WindowPosition.CENTER)

        fixed = Gtk.Fixed()
        win.add(fixed)
        fixed.show()

        videowidget = Gtk.DrawingArea()
        fixed.put(videowidget, 0, 0)
        videowidget.set_size_request(640, 480)
        videowidget.show()

        # Setup GStreamer
        player = gst.element_factory_make("playbin", "MultimediaPlayer")
        bus = player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        #used to get messages that GStreamer emits
        bus.connect("message", on_message)
        #used for connecting video to your application
        bus.connect("sync-message::element", on_sync_message)
        player.set_property("uri", "file://" + os.getcwd() + "/VID/BGA-HABT-001.ogv")
        player.set_state(gst.STATE_PLAYING)

        win.show()

def main():
    Gdk.threads_enter()
    Gtk.main()
    return 0

if __name__ == "__main__":
    Video()
    main()

我总是得到这个错误,连同一个新窗口中的视频打开,而不是现有的窗口。
回溯(最近一次调用):文件“video.py”,第32行,in
打开同步消息
不存在属性“窗口”
如何修复此问题,以便视频显示在我创建的窗口中,而不是显示在新窗口中?
顺便说一下,这个问题是在我从PyGTK 2.24切换到PyGObject 3.0之后才开始出现的。

最佳答案

(转载自GNOME PyGObject 3 Bug Report 663360。答案归Timo Vanwynsberghe)。
有几件事需要注意:
-在你得到它的GdkWindow之前,必须先了解它的绘制区域
-显然,你不能直接得到窗口属性
-需要为XID方法导入GDKX11
有鉴于此,下面是一个最小的工作示例:

from gi.repository import GdkX11, Gtk

class App:
    def __init__(self):
        win = Gtk.Window()
        win.resize(400, 400)
        win.connect('delete-event', Gtk.main_quit)

        da = Gtk.DrawingArea()
        win.add(da)
        win.show_all()

        print da.get_property('window').get_xid()

if __name__ == "__main__":
    App()
    Gtk.main()

10-06 03:16