如何循环打开文件夹

时间:2017-03-03 07:49:28

标签: python tkinter streaming

我构建了一个代码,用于检查文件夹中最新的图片(文件中只有图片),并且它可以处理两张图片。代码将成为更大的视频流代码的一部分,这就是我需要快速切换最新图片的原因。

所以,那就是我曾经尝试过的: 此代码检查两次最近的图片并通过Tkinter打开它。 我现在需要的是使它成为一个在无限循环中运行的代码并在图片之后切换图片。

这是代码:

import tkinter as tk
from PIL import Image, ImageTk
from Tkinter import *
import Image, ImageTk
import glob,os


def RecentFilePath():
    folder = "C:\\NIR"
    return(str(max((x for x in glob.glob(os.path.join(folder,"*")) if os.path.isfile(x)),key=os.path.getmtime)))

root = tk.Tk()

img = ImageTk.PhotoImage(Image.open(RecentFilePath))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
t = True
def callback():
    global t
    t = not t
    if(t):
        img2 = ImageTk.PhotoImage(Image.open(RecentFilePath))
    else:
        img2 = ImageTk.PhotoImage(Image.open(RecentFilePath))
    panel.configure(image = img2)
    panel.image = img2
    root.after(1000, callback)

root.after(1000, callback)
root.mainloop()

非常感谢!!

1 个答案:

答案 0 :(得分:1)

您未在RecentFilePath个作品(PhotoImage和两个img作业)中致电img2

img = ImageTk.PhotoImage(Image.open(RecentFilePath()))
                                                  ^^ you need to call it, to get return value