I've been fiddling with this on and off for a couple of days but just can't seem to come to grips with what the issues might be.
Essentially I'm trying to style some Gtk Widgets in Gtk3 using CSS style declarations, nothing complicated but just trying to target a specific element by its id/name. The Gtk documentation for Gtk.CssProvider() says that
#widgetname { background-color: #333333; }
should work as a valid selector for a widget with its name set to "widgetname" but I just can't seem to make it work. I thought originally that it was the CSS not loading but I can target a top-level widget like so:
GtkWindow { background-color: #333; }
and it will apply the style to the window and I can see the background color has changed. I've tried using the name as an ID for a couple of different types of widgets (GtkEventBox, GtkTextView, GtkStatusBar, GtkBox) and the ID based selector just doesn't seem to work.
Here's a shortened snippet of how I'm loading the css:
css = Gtk.CssProvider()
# css.load_from_file(file)
css.load_from_data('''
GtkWindow {
background-color: #333;
}
GtkEventBox {
background-color: #333;
}
#statusbarwrap, #textview_event_wrap, #box1 {
background-color: #333;
}
''')
style_context = self.get_style_context()
style_context.add_provider(
css,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
Here is the docs for GtkCssProvider https://developer.gnome.org/gtk3/3.7/GtkCssProvider.html
Example 24 on that page (just scroll down a page or two) shows that the #ID selector is valid and I'm setting the name for the widgets in Glade.
Any help would be greatly appreciated.
name
property and not theid
attribute. See Locoluis's answer. – Hyder