开发者

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?


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜