我想在目录中显示files.gif列表中的图片,图片应该每3秒更改一次。我确实尝试了不同的解决方案,但要么只显示第一张照片或没有。
TIA
from tkinter import *
import os
path = os.getcwd()
arr = []
for files in next(os.walk('/home/vimart/Python/img/'))[2]:
arr.append('/home/vimart/Python/img' + "/" + files)
canvas_width = 300
canvas_height =300
master = Tk()
canvas = Canvas(master,
width=canvas_width,
height=canvas_height)
canvas.pack()
def display():
canvas.create_image(20,20, anchor=NW, image=canvas.img)
def get_picture():
for picture in arr:
canvas.img = PhotoImage(picture)
master.after(3000, display)
get_picture()
mainloop()
我认为它不需要描述。
import tkinter as tk
from PIL import ImageTk
import os
# --- functions ---
def get_filenames(path):
result = []
#for one_file in os.listdir(path):
for one_file in next(os.walk(path))[2]:
if one_file.lower().endswith('.gif'): # sugessted by Nae
result.append(path + one_file)
return result
def display():
global current_index
picture = arr[current_index]
canvas.img = ImageTk.PhotoImage(file=picture)
canvas.create_image(20,20, anchor='nw', image=canvas.img)
current_index = (current_index + 1) % len(arr)
master.after(3000, display)
# --- main ---
path = '/home/vimart/Python/img/'
arr = get_filenames(path)
current_index = 0
canvas_width = 300
canvas_height = 300
master = tk.Tk()
canvas = tk.Canvas(master, width=canvas_width, height=canvas_height)
canvas.pack()
display()
master.mainloop()