PyGTK 设置窗口图标与库存图片
我觉得这应该很简单,但我想我错过了一些东西。
所以我想用其中一张库存图像设置窗口的图标。我尝试过:
windowIcon = gtk.image_new_form_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_MENU)
window.set_icon(windowIcon.get_pixbuf())
Python 然后抱怨:
文件“./sample.py”,第 44 行,init
window.set_icon(windowIcon.get_pixbuf())
ValueError:图像应该是 GdkPixbuf 或空
我尝试将 gtkImage 转换为 GdkPixbuf 因为当我没有 python 抱怨时
类型错误:图标应该是 GdkPixbuf 或 None
在 pygtk 文档中它说:
如果图像的存储类型不是 gtk.IMAGE_EMPTY 或 gtk.IMAGE_PIXBUF,则会引发 ValueError 异常。
所以我猜测库存图像的存储类型是错误的。问题是我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在任何 GtkWidget 上使用 .render_icon() 方法来提供库存项目作为图标。
You can use the .render_icon() method on any GtkWidget to provide a stock item as the icon.
render_icon()
方法的工作原理如上所示。您的代码有问题,因为get_pixbuf()
方法返回None
。这是因为图像不是从pixbuf
创建的,并且由于某种原因,它不会转换它。从图像获取pixbuf
的另一种方法是使用gtk.gdk.pixbuf_new_from_image()
函数。The
render_icon()
method works as given above. Your code was having problems, because theget_pixbuf()
method was returningNone
. This is because the image was not created from apixbuf
, and for some reason, it won't convert it. Another way of getting apixbuf
from an image is to use thegtk.gdk.pixbuf_new_from_image()
function.