PhotoImage: iterating over a list of images

Alex Martelli aleax at aleax.it
Sun Oct 6 05:51:04 EDT 2002


Ian Oeschger wrote:

> Why does the example below display only img4.gif and not any of
> the others. All of them are sitting in the same directory. Is there
> something wrong about the way that I iterate over the list??

No, the problem is that being JUST used in an image= does not
suffice to keep a PhotoImage object alive -- think of it as a weak
reference (though the real underlying issue is different, it works
much the same way).  Simplest fix, e.g.:

_images = {}
def makeBtn(icon):
    _images[icon]=PhotoImage(file=icon)
    Button(root, width=24, height=24, image=_images[icon],
        command=sys.exit).pack(side=RIGHT, padx=5, pady=5)


Alex




More information about the Python-list mailing list