What is a laid-back way to display a stock icon in GTK3?

I compile a GUI using PyGObject. This Python code works in context. I get a toolbar button with an open icon.

from gi.repository import Gtk # ... toolbar = Gtk.Toolbar() toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR) # ... self.fileOpen = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OPEN) self.fileOpen.connect("clicked", self.on_FileOpenStandard_activate) toolbar.insert(self.fileOpen, -1) 

The image shows the icon that appears in the toolbar.

But according to this resource , new_from_stock() deprecated:

Deprecated since version 3.10: Gtk.ToolButton.new () Gtk.ToolButton.new () .

Good. So, after I rummaged on, this is what I came up with for the replacement:

 self.fileOpen = Gtk.ToolButton( Gtk.Image.new_from_icon_name("document-open", Gtk.IconSize.LARGE_TOOLBAR), "Open") self.fileOpen.connect("clicked", self.on_FileOpenStandard_activate) toolbar.insert(self.fileOpen, -1) 

But this is the result:

The image shows an icon that does not appear on the toolbar.

What is the correct way to do this, which is still supported by the current GTK library?

+6
source share
1 answer

Looking at this C ++ GitHub example , I am surprised to find a direct call to the static function new() , not the constructor.

So I decided to give it a try. Look carefully at the difference. It is subtle.

  #vvv self.fileOpen = Gtk.ToolButton.new( Gtk.Image.new_from_icon_name("document-open", Gtk.IconSize.LARGE_TOOLBAR), "Open") self.fileOpen.connect("clicked", self.on_FileOpenStandard_activate) toolbar.insert(self.fileOpen, -1) 

To my surprise, this displays an icon where there is no other approach.

Bonus: Clean version above:

 # iconSize to be reused iconSize = Gtk.IconSize.LARGE_TOOLBAR # ... openIcon = Gtk.Image.new_from_icon_name("document-open", iconSize) self.fileOpen = Gtk.ToolButton.new(openIcon, "Open") self.fileOpen.connect("clicked", self.on_FileOpenStandard_activate) toolbar.insert(self.fileOpen, -1) 
+4
source

All Articles