How to set bg image of a window in a GTK3 applicat

2019-09-11 11:39发布

问题:

I found this way:

GdkPixmap *backPixMap = gdk_pixmap_create_from_xpm ( window , NULL , NULL , fileName );
gdk_window_set_back_pixmap( GTK_WIDGET( window )->window , backPixMap , FALSE );

but it seems that GdkPixmap is obsolete now...

So, with GTK3, how can I set the background image of a GtkWindow?

回答1:

You just need to use an Overlay. Following is an example.

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        self.overlay = Gtk.Overlay()
        self.add(self.overlay)

        self.bg = Gtk.Image()
        # Load file. Optionally scale it for window
        self.bg.set_from_file(BACKGROUND_IMAGE)
        # Wrapping in the Scrollable make it resizable.
        scrollable_wrapper = Gtk.ScrolledWindow()
        scrollable_wrapper.add(self.bg)
        scrollable_wrapper.set_size_request(700, 500)

        self.overlay.add(scrollable_wrapper)
        text = Gtk.Label("Test")
        self.overlay.add_overlay(text)

        self.connect('destroy', lambda w: Gtk.main_quit())
        self.show_all()


Window()
Gtk.main()


标签: gtk gdk gtk3