PyGTK set icon of window with stock image

2.8k Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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)
0
On

The render_icon() method works as given above. Your code was having problems, because the get_pixbuf() method was returning None. This is because the image was not created from a pixbuf, and for some reason, it won't convert it. Another way of getting a pixbuf from an image is to use the gtk.gdk.pixbuf_new_from_image() function.