Image on a button

The only reference to the image object is a local variable. When __init__ exits, the local variable is garbage collected so the image is destroyed. In the second example, because the image is created at the global level it never goes out of scope and is therefore never garbage collected.

To work around this, save a reference to the image. For example, instead of photo use self.photo.

Leave a Comment