As most of us know in tkinter in order to properly display an image it must be assigned to a variable, however what if you don't know that amount of images that are going to be necessary? i.e. A friend list, how would one display the friends pictures?

It does, but I'm stuck in a pattern of thinking I can't get out of with the use of it, here's what I've been trying:

self.box=Listbox(self)
        self.box.pack()
        list1=['icon_man.gif','group_icon.gif']
        for img in list1:
            imag=PhotoImage(file=img)
            self.box.insert(END,imag)

This just puts the word (pyimage"whatever number") in the box, which makes sense "unfortunately" any alternative ideas?


And to make sure that it wasn't just the listbox being an issue

def create_widget(self):
        self.lab=Label(self)
        self.lab.pack()
        list1=['icon_man.gif','group_icon.gif']
        for file in list1:
            word=PhotoImage(file=file)
            self.lab['image']=word

Edited 5 Years Ago by pyguy62: n/a

I do not know details by heart, but for this part I would think that code would go more like:

self.box=Listbox(self)
        self.box.pack()
        list1=[PhotoImage(file=img) for img in 'icon_man.gif','group_icon.gif']
        for imag in list1:
            self.box.insert(END,imag)

Maybe even with generator expression:

self.box=Listbox(self)
        self.box.pack()
        for imag in (PhotoImage(file=img) for img in 'icon_man.gif','group_icon.gif'):
            self.box.insert(END,imag)

Edited 5 Years Ago by pyTony: n/a

It seems the best way, at least to me, is to have a set amount labels with images on the page, and have something like a next button that will reassign the image value of the label.

This article has been dead for over six months. Start a new discussion instead.