0

我正在开发一个使用的GTK+前端libvlc。到目前为止,我遵循了这个指南,它工作正常,除了当我调整窗口大小时,正如你在图片中看到的那样,它变得一团糟(我不知道这种现象可能有一个词)。pythonpython-vlcDrawingArea


搞砸了 GtkDrawingArea


我在控制台中收到这些警告,但不确定这是否相关:

[00007fce1c014eb0] main filter error: Failed to create video converter
[00007fce2807ff70] vdpau_avcodec generic error: Xlib is required for VDPAU

css我已经尝试使用样式设置窗口的背景颜色,GTK+但没有效果。

我认为这不应该发生,我错过了什么吗?顺便说一句,我在wayland

4

2 回答 2

1

使用 Google 并检查不同的示例,我创建了DrawingArea用黑色填充背景的代码。

将绘图功能分配给DrawingArea

self.draw_area = Gtk.DrawingArea()
self.draw_area.connect("draw", self.da_draw_event)

填充区域的功能

def da_draw_event(self, widget, cairo_ctx):
    cairo_ctx.set_source_rgb(0, 0, 0)
    cairo_ctx.paint()

完整代码

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

import vlc

MRL = ""

class ApplicationWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Python-Vlc Media Player")
        self.player_paused=False
        self.is_player_active = False
        self.connect("destroy", Gtk.main_quit)

    def show(self):
        self.show_all()

    def setup_objects_and_events(self):
        self.playback_button = Gtk.Button()
        self.stop_button = Gtk.Button()

        self.play_image = Gtk.Image.new_from_icon_name(
                "gtk-media-play",
                Gtk.IconSize.MENU
            )
        self.pause_image = Gtk.Image.new_from_icon_name(
                "gtk-media-pause",
                Gtk.IconSize.MENU
            )
        self.stop_image = Gtk.Image.new_from_icon_name(
                "gtk-media-stop",
                Gtk.IconSize.MENU
            )

        self.playback_button.set_image(self.play_image)
        self.stop_button.set_image(self.stop_image)

        self.playback_button.connect("clicked", self.toggle_player_playback)
        self.stop_button.connect("clicked", self.stop_player)

        self.draw_area = Gtk.DrawingArea()
        self.draw_area.set_size_request(300, 300)
        self.draw_area.connect("realize",self._realized)
        self.draw_area.connect("draw", self.da_draw_event)

        self.hbox = Gtk.Box(spacing=6)
        self.hbox.pack_start(self.playback_button, True, True, 0)
        self.hbox.pack_start(self.stop_button, True, True, 0)

        self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(self.vbox)
        self.vbox.pack_start(self.draw_area, True, True, 0)
        self.vbox.pack_start(self.hbox, False, False, 0)

    def da_draw_event(self, widget, cairo_ctx):
        #print('da_draw_event')
        #print('widget:', widget)
        #print('cairo_ctx:', cairo_ctx)

        cairo_ctx.set_source_rgb(0, 0, 0)
        cairo_ctx.paint()

    def stop_player(self, widget, data=None):
        self.player.stop()
        self.is_player_active = False
        self.playback_button.set_image(self.play_image)

    def toggle_player_playback(self, widget, data=None):

        """
        Handler for Player's Playback Button (Play/Pause).
        """

        if self.is_player_active == False and self.player_paused == False:
            self.player.play()
            self.playback_button.set_image(self.pause_image)
            self.is_player_active = True

        elif self.is_player_active == True and self.player_paused == True:
            self.player.play()
            self.playback_button.set_image(self.pause_image)
            self.player_paused = False

        elif self.is_player_active == True and self.player_paused == False:
            self.player.pause()
            self.playback_button.set_image(self.play_image)
            self.player_paused = True
        else:
            pass

    def _realized(self, widget, data=None):
        self.vlcInstance = vlc.Instance("--no-xlib")
        self.player = self.vlcInstance.media_player_new()
        win_id = widget.get_window().get_xid()
        self.player.set_xwindow(win_id)
        self.player.set_mrl(MRL)
        self.player.play()
        self.playback_button.set_image(self.pause_image)
        self.is_player_active = True

if __name__ == '__main__':
    if not sys.argv[1:]:
       print("Exiting \nMust provide the MRL.")
       sys.exit(1)
    if len(sys.argv[1:]) == 1:
        MRL = sys.argv[1]
        window = ApplicationWindow()
        window.setup_objects_and_events()
        window.show()
        Gtk.main()
        window.player.stop()
        window.vlcInstance.release()
于 2019-05-18T13:17:06.140 回答
0

正如@furas 在评论中指出的那样,不断在DrawingArea作品上绘制一个黑色矩形非常好。widget我在派生自的 a 中使用以下代码Gtk.DrawingArea

def draw(self, c, *args):
    rect = self.get_allocation()
    win = self.get_window().get_position()
    c.set_source_rgb(0, 0, 0)
    c.rectangle(rect.x - win.x, rect.y - win.y, rect.width, rect.height)
    c.fill()
self.connect("draw", draw)

编辑:请参阅@furas 答案以获得更简单的方法。

于 2019-05-18T11:55:07.500 回答