mpv如何渲染到GTK4窗口或小部件? 在 GTK3 中,很容易获得 XID(并将其作为 MPV 的参数),但在 GTK4 中,似乎必须使用 GtkX11 和 X11Surface https://docs.gtk.org/gdk4-x11/method.X11Surface 来完成。 get_xid.html 但我不知道如何在 python 中做到这一点 - 无法从窗口/小部件获取表面。
#!/usr/bin/env python3
import gi
import mpv
gi.require_version('Gtk', '4.0')
gi.require_version('Gdk', '4.0')
gi.require_version('GdkX11', '4.0')
from gi.repository import Gtk, Gdk, GdkX11
class MainClass(Gtk.ApplicationWindow):
def __init__(self, app):
super(MainClass, self).__init__()
self.set_application(app)
self.set_default_size(600, 400)
self.connect("destroy", self.on_destroy)
widget = Gtk.Frame()
self.set_child(widget)
self.present()
# Can't get XID from widget there
self.mpv = mpv.MPV(wid=str(GdkX11.X11Surface.get_xid(widget)))
self.mpv.play("test.webm")
def on_destroy(self, widget, data=None):
self.mpv.terminate()
Gtk.main_quit()
def on_activate(app):
application = MainClass(app)
if __name__ == '__main__':
# This is necessary since like Qt, Gtk stomps over the locale settings needed by libmpv.
# Like with Qt, this needs to happen after importing Gtk but before creating the first mpv.MPV instance.
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
app = Gtk.Application()
app.connect('activate', on_activate)
app.run(None)
TypeError: argument self: Expected GdkX11.X11Surface, but got gi.repository.Gtk.Frame
我不知道这是否完全正确
GdkX11.X11Surface.get_xid() 采用 X11Surface 作为参数
试试这个:
self.mpv = mpv.MPV(wid=str(GdkX11.X11Surface.get_xid(self.get_surface())))
这对我有用
您可能会启用 Wayland,因此请记住运行
GDK_BACKEND=x11 ./your-script.py
这应该可以解决问题:
wid = GdkX11.X11Surface.get_xid(self.get_surface())
options = {'vo': 'x11', 'hwdec': 'no', 'cursor-autohide': 1000, 'force-window': True, 'wid': wid}