0

我正在尝试使用 Tkinter 创建一个程序,该程序在窗口中显示来自几个不同目录的缩略图。到目前为止,我有这个:

import Tkinter as tk
from PIL import Image, ImageTk
import Image, os

root = tk.Tk()
root.title('Shot Viewer')
w, h, x, y = 1000, 1000, 0, 0
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

#quit
def quit(root):
    root.quit()
    root.destroy()

path = "/media/Expansion Drive/Heros Mission 3/Scenes/Scene 1-3/Shots/"
labels = []
for files in os.listdir(path):
    number = files.split("_")[1]
    filed = "/media/Expansion Drive/Heros Mission 3/Scenes/Scene 1-3/Shots/Shot_{} /Frames/Shot_{}_000000.png".format(number, number)
    if os.path.lexists(filed) == 'False':
        pass
    else:
        im = Image.open(imageFile)
        im.thumbnail((96, 170), Image.ANTIALIAS)
        image = ImageTk.PhotoImage(im)
        label = tk.Label(root, image=image, name=number)
        labels.append(label)

print labels

for label in labels:
    panel = label.grid()

panel2.grid(row=2, column=1)
button2 = tk.Button(panel2, text='Quit', command=lambda root=root:quit(root))
button2.grid(row=1, column=1, sticky='NW')

root.mainloop()

但是这不起作用,有人有什么建议吗?

谢谢汤姆

4

2 回答 2

1

使用 glob 模块帮助查找相关文件。

至于无法出现的图像:

import Tkinter as tk
from PIL import Image, ImageTk
import glob

root = tk.Tk()

labels = []

for jpeg in glob.glob("C:/Users/Public/Pictures/Sample Pictures/*.jpg")[:5]:
    im = Image.open(jpeg)
    im.thumbnail((96, 170), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(im)
    label = tk.Label(root, image=photo)
    label.pack()    
    label.img = photo # *
    # * Each time thru the loop, the name 'photo' has a different
    # photoimage assigned to it.
    # This means that you need to create a separate, 'longer-lived'
    # reference to each photoimage in order to prevent it from
    # being garbage collected.
    # Note that simply passing a photoimage to a Tkinter widget
    # is not enough to keep that photoimage alive.    
    labels.append(label)

root.mainloop()
于 2013-03-11T23:21:08.017 回答
0

我不认为你在你所说的地方正确处理它panels = label.grid()。相反,试着这样做label.grid,它不是一个赋值运算符,而是一个动作。

于 2013-03-08T22:48:18.190 回答