简体   繁体   English

Tkinter 图像查看器

[英]Tkinter Image viewer

I'm trying to create a program using Tkinter that displays a thumbnail from several different directories in on window.我正在尝试使用 Tkinter 创建一个程序,该程序在窗口中显示来自几个不同目录的缩略图。 So far I have this:到目前为止,我有这个:

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()

However this is not working, does anyone have any suggestions?但是这不起作用,有人有什么建议吗?

Thanks Tom谢谢汤姆

Use the glob module to help find the relevant files.使用 glob 模块帮助查找相关文件。

As for images failing to appear:至于无法出现的图像:

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()

I do not think you are handling it correctly where you say panels = label.grid() .我认为您在说panels = label.grid()地方没有正确处理它。 Instead, try to just do label.grid so it is not an assignment operator but an action.相反,试着只做label.grid所以它不是赋值运算符而是一个动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM