After adding “primary toolbar” style class to a gtk.Builder object, gtk.ToolButtons in toolbar are not primary-themed

StackOverflow https://stackoverflow.com/questions/12204607

  •  29-06-2021
  •  | 
  •  

Question

I've been trying to get my toolbar to look like the standard toolbars in stock Ubuntu apps, and it works to an extent, except the buttons in the toolbar do not have the correct theming applied to them. The text is colored for the default toolbar, and on hover the buttons are an ugly tan color instead of getting darker grey as they do in the standard Ubuntu apps.

In other words I'm getting this:

Instead of this:

(I don't have enough rep to post images directly)

I've been using the following method to change the toolbar style, since Glade doesn't seem to have an option to change it itself:

self.bd = Gtk.Builder()
self.bd.add_from_file("builderfile.glade")
self.bd.connect_signals(self)
...

toolb = self.bd.get_object("toolbar")
toolb_style = toolb.get_style_context()
toolb_style.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)

This works to the extent that the toolbar is now darkened, but the buttons are incorrectly themed. I tried adding buttons to the toolbar manually after setting the STYLE_CLASS_PRIMARY_TOOLBAR instead of using Gtk.Builder() but they weren't the right color either.

I suppose manually creating a toolbar and filling out all the information might work, but it would be nice to be able to use Glade for the design.

Was it helpful?

Solution

I'm writing this answer here since it's a solution, but it's not ideal because it doesn't use Glade and it's kind of messy. I'll have to wait until a new build of Glade gets into Ubuntu, since apparently the feature has been added upstream.

The solution I eventually used was just to do all the toolbar creation in standard GTK and python, and then add it to an empty Gtk.Box defined in Glade. Like this:

bd = Gtk.Builder()
bd.add_from_file("gladefile.glade")

...

button1 = Gtk.ToolButton(stock_id=Gtk.STOCK_ADD, ...)
button2 = Gtk.ToolButton(...)

toolbar = Gtk.Toolbar()
toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
toolbar.insert(button1, 0)
toolbar.insert(button2, 1)
toolbar.show_all()

bd.get_object("toolbar_slot").pack_start(toolbar, False, True, 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top