pythongtkiconspygtkgdkpixbuf

PyGTK set icon of window with stock image


I feel like this should be pretty simple, but I guess I am missing something.

So I want to set the icon of a window with one of the stock images. I have tried:

windowIcon = gtk.image_new_form_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_MENU)
window.set_icon(windowIcon.get_pixbuf())

Python then complains that:

File "./sample.py", line 44, in init
window.set_icon(windowIcon.get_pixbuf())
ValueError: image should be a GdkPixbuf or empty

I try to convert the gtkImage to a GdkPixbuf because when I didn't python complained that

TypeError: icon should be a GdkPixbuf or None

In the pygtk documentation it says:

If the storage type of the image is not either gtk.IMAGE_EMPTY or gtk.IMAGE_PIXBUF the ValueError exception will be raised.

So I guessing that storage type of the stock image is wrong. The question is how to I get around this?


Solution

  • You can use the .render_icon() method on any GtkWidget to provide a stock item as the icon.

    windowicon = window.render_icon(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_MENU)
    window.set_icon(windowicon)