Search code examples
imagewidgetpython-2.7pygtkglade

replace image using gtk.image.set_from_file


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


Solution

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