grijalvaromero
grijalvaromero

Reputation: 599

Set CSS style to GtkBox with python

is posible change the background color of an object GtkBox? so far all the styles I have worked well in other objects but GtkBox not work for me You know why? this is my code

self.Ventana = self.builder.get_object("ventana")
self.Header= self.builder.get_object("header")
self.Ventana.set_name('MyWindow')
self.Header.set_name('header')
self.style_provider = Gtk.CssProvider()
self.css=open(self.ruta+'/css/estilos.css','rb')
self.css_data = self.css.read()
self.style_provider.load_from_data(self.css_data)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),self.style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

/*********CSS STYLES *********/
#MyWindow {
    background-color: #1d1d1d;
}
/*i try whit: #MyWindow #header, #MyWindow GtkBox */
#header{
    background-color: #ffffff;
    border-bottom: 1px solid white;
}

Upvotes: 0

Views: 600

Answers (1)

elya5
elya5

Reputation: 2258

The problem is that not all gtk widgets draw their own background. The background is then determined by the underlying widget. This behavior was changed for some widgets with gtk+ 3.12.

So changing the background color of a Gtk.Box does not work for version < gtk+ 3.12. If you are using an older version, an alternative approach would be to use a Gtk.Viewport underneath the Gtk.Box and change the color of the viewport.

To put it into some code:

box = Gtk.Box()
viewport = Gtk.Viewport()
viewport.add(box)
#and the CssProvder, StyleContext,...

CSS:

GtkViewport {
    background-color: red;
)

Upvotes: 1

Related Questions