Search code examples
pythongtk3libvlcpython-vlc

python-vlc will not embed gtk widget into window, but open a new window instead


I'm working on a gtk3 front end for libvlc written in python using python-vlc. I'm following the gtk3 example from the python-vlc github page, but am experiencing strange behavior. I have a widget that looks like that:

import gi
import sys
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class PlayerWidget(Gtk.DrawingArea):

    __gtype_name__ = 'VLCWidget'

    def __init__(self, instance):

        Gtk.DrawingArea.__init__(self)
        self.player = instance.media_player_new()

        def handle_embed(*args):
            if sys.platform == 'win32':
                self.player.set_hwnd(self.get_window().get_handle())
            else:
                self.player.set_xwindow(self.get_window().get_xid())
            return True
        self.connect("realize", handle_embed)
        self.set_size_request(320, 200)

I embed it here:

import vlc
import sys
from widgets.player import PlayerWidget
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class VideoPlayer(Gtk.Window):
    CONST_APP_NAME = "video player"

    def __init__(self):
        Gtk.Window.__init__(self)
        if 'linux' in sys.platform:
            self.instance = vlc.Instance("--no-xlib")
        else:
            self.instance = vlc.Instance()

        self.set_default_size(800, 600)
        header = Gtk.HeaderBar(title=self.CONST_APP_NAME)
        header.set_subtitle("Filename.mp4")
        header.set_show_close_button(True) # this one is the troublemaker
        self.set_titlebar(header)
        self.connect("destroy", Gtk.main_quit)

        self.player_widget = PlayerWidget(self.instance)
        self.add(self.player_widget)

    def show_window(self):
        self.show_all()
        Gtk.main()

    def set_media(self, fname):
        self.player_widget.player.set_media(self.instance.media_new(fname))

    def play(self):
        self.player_widget.play()


if not len(sys.argv) > 0:
    print('Please provide a filename')
    sys.exit(1)

p = VideoPlayer()
p.set_media(sys.argv[1])
p.play()
p.show_window()
p.instance.release()

It works fine if I embed it into an empty Gtk.window. If, however, I add a HeaderBar to that window as well and then add a close button to that HeaderBar using set_show_close_button(True) it stops working as expected. The PlayerWidget will not be shown embedded anymore, but instead a new (second) window will be opened where the video is played. If I do not add the close button to the HeaderBar the widget gets embedded just fine.

A warning is thrown to the console: xcb_window window error: X server failure

I first thought it could be because I use gnome under wayland, but it occurs on X as well as on wayland.

Any help is appreciated.

Update 1: Added full code example. When I ran it today, the first time it actually worked as expected, but after that the same bug as described above occured again. Very weird.


Solution

  • As @mtz and @stovfl correctly pointed out, the problem was that I started the video playback (p.play()) before creating the window (p.show_window()).

    As suggested I used GLib.idle_add(p.play) to let the window start the playback once it's ready. The GLib module can be imported using from gi.repository import GLib.