Adding an icon to a ToolButton in Gtk 3

2019-05-24 08:20发布

问题:

Is there a way to add an icon to a Gtk.ToolButton (Gtk3 using PyGi for Python) to add to the GTK+3 toolbar ?

Below is my code:

self.addfile = Gtk.ToolButton()
self.addfile.set_label("Add File")
self.addfileimg = Gtk.Image()
self.addfileimg.show()
self.addfileimg.set_from_file(self.get_resource("img/file.png"))
self.addfile.set_icon_widget(self.addfileimg)
self.addfile.connect("clicked", self.on_open_file)

Note: The get_resource() method digs into the local working folder for the resource path and this method is assumed working in this context.

I tried the code written above using PyGi. The image file is valid and everything is working but the image doesn't appear.

回答1:

There is a method of the GtkToolButton class named set_icon_widget, taking a GtkWidget instance as its operand. GtkImage is a container that holds an image. Thus:

myToolButton.set_icon_widget(myImageWidget)