使用gtk.image.set_from_file替换图像 [英] replace image using gtk.image.set_from_file
问题描述
我正在尝试添加,并且如果用户需要,请从python中的小部件更改图像.
I'm trying to add and if needed by user, change the image from a widget in python.
我正在将Glade与gtk2 +,python 2.7.3一起使用,而这正是我正在做的事情
I'm using Glade with gtk2+, python 2.7.3, and these is what I'm doing
image = gtk.Image()
image.set_from_file("MyImagePath.png")
image.show()
button = App.get_object('buttonExample')
button.add(image)
这就是我尝试更改图像时得到的
and this is what I get when try to change the image
GtkWarning:尝试将类型为GtkImage的小部件添加到 GtkAspectFrame,但作为GtkBin子类,GtkAspectFrame只能 一次包含一个小部件;它已经包含一个类型的小部件 GtkImage
GtkWarning: Attempting to add a widget with type GtkImage to a GtkAspectFrame, but as a GtkBin subclass a GtkAspectFrame can only contain one widget at a time; it already contains a widget of type GtkImage
图像已按预期正确加载,但是如果更改图像的输入路径,希望可以更改按钮图像,但这不是我要的.
The image is loaded correctly as expected, but if I change the input path for image, I wish I could change the button image, but this is not what I'm getting.
我尝试使用gtk.Image.clear(),但是它说我不能在按钮上使用它(也许只是把事情弄乱了)
I tried to use gtk.Image.clear(), but it says I cannot use it on a button (maybe im just messing things around)
是否存在将图像加载和重新加载到按钮的好方法?
Is there a good way to load and reload images to a button?
Thx
推荐答案
您必须先从按钮上删除旧图像,然后再添加新图像(错误消息非常简单),或者您可以尝试更改当前图像:
You either have to remove the old image from the button before adding a new one (the error message is pretty straightforward about this), or you could try changing the current image:
button.get_child().set_from_file("MyImagePath.png")
这篇关于使用gtk.image.set_from_file替换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!