Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
Kevin S Avatar asked Apr 21 '11 15:04

Kevin S


2 Answers

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)
like image 109
Andrew Steele Avatar answered Sep 22 '22 10:09

Andrew Steele


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.

like image 41
anon Avatar answered Sep 20 '22 10:09

anon