Ok my script takes a list of files picks out all jpgs and stores it in files[] now the problem is how do I display all thous images in tk gui window below is what i have that works for 1 image how do i make this work for basically an unlimited amount of images.

for file in files[:]:
        image = Image.open(filedir+"/"+file)
        ##((width, height))
        image.thumbnail((160, 240))
        photo = ImageTk.PhotoImage(image)
        label = Label(image=photo)
        label.image = image # keep a reference!
        label.pack()

        
        print files
    return

Recommended Answers

All 10 Replies

Hint:
Looks to me like you are creating the same label over and over again.

Ok yeah what would be the right way to set it up for multiply images

would a do something like
count += 1
then lable+(count) = whatever

You could take each labelobject an append it to a list.

Try something like this:

label = list(range(len(files)))
    for k, fname in enumerate(files):
        image = Image.open(filedir+"/"+fname)
        ##((width, height))
        image.thumbnail((160, 240))
        photo = ImageTk.PhotoImage(image)
        label[k] = Label(image=photo)
        label[k].image = image # keep a reference!
        #label[k].pack()  # pack when you want to display it


        #print files
    return ...
commented: Thank you so much, I want to display multiple images, but today I have seen you have figured it out 11 years ago, really thank you so much! +0

thanks I was as trying to do image.append and it wasent working

Is this line correct
label[k] = Label(picSet, image=photo)
if earlier in the code
picSet = Tk()

I get TclError: image "pyimage75" doesn't exist

However if i remove picSet it puts large spaces in the root window but no pics showup

label = list(range(len(files)))
    for k, fname in enumerate(files):
        image = Image.open(filedir+"/"+fname)
        ##((width, height))
        image.thumbnail((160, 240))
        photo = ImageTk.PhotoImage(image)
        label[k] = Label(picSet, image=photo)
        label[k].image = image # keep a reference!
        label[k].pack()  # pack when you want to display it
        picSet.mainloop()

in the process of debugging i separated the problem of the script now this gives me the blank spaces in the root window but no images and no errors

from  os import *
from Tkinter import *
import tkFileDialog
from PIL import Image, ImageTk

root = Tk()
root.title("BIEC Picture Viewer")
title = Label(root, text="Select a Set of images")



filedir = tkFileDialog.askdirectory()
files = listdir(filedir)
for file in files[:]:
    if(file.split(".")[-1].upper() != 'JPG'):
        files.remove(file)
label = list(range(len(files)))
for k, fname in enumerate(files):
    image = Image.open(filedir+"/"+fname)
    ##((width, height))
    image.thumbnail((160, 240))
    photo = ImageTk.PhotoImage(image)
    label[k] = Label(root, image=photo)
    label[k].image = image # keep a reference!
    label[k].pack()  # pack when you want to display it
picSet.mainloop()

Some ideas. First, tkinter does not display all image types. I do not think that it will display a JPG but am not sure. Convert some pictures to a gif and see it that works. Second, "file" is a reserved word and so should not be a variable name, Third, you are not iterating through the entire list of files because of the delete in the for loop. Append the names you want to keep to a second list. Also, you don't have to create a list of n elements, just append as you go along (see below). Finally, if you don't know about effbot, it is a good reference. http://effbot.org/tkinterbook/label.htm

label_list = []
for k, fname in enumerate(files):
    image = Image.open(filedir+"/"+fname)
    ##((width, height))
    image.thumbnail((160, 240))
    photo = ImageTk.PhotoImage(image)
    this_label = Label(root, image=photo) 
    this_label.pack()  # pack when you want to display it
    label_list.append( this_label )
picSet.mainloop()

Ok i see now the reference was being removed thanks for the the script very help full i also like how there is less loops much better than my poor attempt.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.