replace image using gtk.image.set_from_file

妖精的绣舞 提交于 2019-12-11 19:39:18

问题


I'm trying to add and if needed by user, change the image from a widget in python.

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: 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.

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


回答1:


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")



回答2:


This was exactly what I needed. I used the clear in the image as you said and also cleaned the button image, as follows:

App.get_object('buttonExample').remove(image)
image.clear()

Thank you very much for you help!



来源:https://stackoverflow.com/questions/11461285/replace-image-using-gtk-image-set-from-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!