DrawingArea无法获取XID

发布于 2024-12-13 05:58:41 字数 2866 浏览 1 评论 0原文

我有以下 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 行,位于 同步消息 win_id = videowidget.window.xid AttributeError:“DrawingArea”对象没有属性“window”

如何解决此问题,以便视频显示在我创建的窗口中,而不是新窗口中?

顺便说一下,这个问题是在我从 PyGTK 2.24 切换到 PyGObject 3.0 后才开始出现的。

I have the following Python 2.7/PyGObject 3.0/PyGST 0.10 module:

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

I am always getting this error, along with the video opening in a new window, instead of the existing window.

Traceback (most recent call last): File "video.py", line 32, in
on_sync_message
win_id = videowidget.window.xid AttributeError: 'DrawingArea' object has no attribute 'window'

How do I fix this, so the video displays in the window I created, instead of a new one?

By the by, this problem only started occuring after I switched to PyGObject 3.0 from PyGTK 2.24.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2024-12-20 05:58:41

(转载自 GNOME PyGObject 3 Bug 报告 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()

(Reprinted from GNOME PyGObject 3 Bug Report 663360. Answer credit goes to Timo Vanwynsberghe).

There are a couple of things to note:
- the drawingarea has to be realised before you can get its GdkWindow
- apparently, you can't get the window property directly
- you need to import GdkX11 for the xid method

With this in mind, here is a minimal working example:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文