I am trying to add a Gtk stock icon to my window. Here is the code:

#! /usr/bin/python

from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title='My Window Title')

        self.icon = self.render_icon(Gtk.STOCK_FLOPPY, 1)

        self.connect('delete-event', Gtk.main_quit)

win = MyWindow()

According to this page icon size should be one of Gtk icon size constants but Eclipse code completion doesn't show any of the listed values and if I manually enter one of them I get

AttributeError: 'gi.repository.Gtk' object has no attribute 'ICON_SIZE_SMALL_TOOLBAR'

If I set the icon size to the literal 1 the script works. I'm sure this has to do something with Gtk or PyGtk versions, but I can't figure what I am doing wrong. Thanks in advance and sorry for the novice question.


The problem is that you are looking at the PyGTK documentation, but are using PyGI/GTK3 in your script.

The docs on gtk_widget_render_icon() says the following:

gtk_widget_render_icon has been deprecated since version 3.0 and should not be used in newly-written code. Use gtk_widget_render_icon_pixbuf() instead.

Then the docs on gtk_widget_render_icon_pixbuf() says this:

gtk_widget_render_icon_pixbuf has been deprecated since version 3.10 and should not be used in newly-written code. Use gtk_icon_theme_load_icon() instead.

Great huh?! So let's move on.

gtk_icon_theme_load_icon() takes the following arguments:

  • icon_name : the name of the icon to lookup
  • size : the desired icon size. The resulting icon may not be exactly this size
  • flags : flags modifying the behavior of the icon lookup

Knowing this, you can change your old render_icon line with the following:

        icontheme = Gtk.IconTheme.get_default()
        self.icon = icontheme.load_icon(Gtk.STOCK_FLOPPY, 128, 0)

Which will lookup a 128x128 px image. You might want to change the flags if needed, the enum is documented here. This can be translated into Python like this:

# And so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.