I would like to display multiple images in tkinter canvas looping through at some fixed interval. It works fine without recursiveness.
Here's my code so far, appreciate any suggestion.
from tkinter import *
from PIL import Image, ImageTk
picNames = [f"{x}.png" for x in 'abcd'] # all pics are having equal shape
w, h = (lambda picNames: Image.open(picNames[0]).size)(picNames) # (640, 640)
lag = 2000
# make GUI
tk = Tk()
tk.title('chessGUI')
canvas = Canvas(tk, width=w, height=h, highlightthickness=0, borderwidth=0)
canvas.grid()
bg_col = 'gray15'
canvas.create_rectangle(0, 0, w, h, fill=bg_col, outline=bg_col)
canvas.config(cursor='tcross')
# main loop
picNameIterator = (x for x in picNames)
def update_ip():
global picNameIterator
try:
picName = next(picNameIterator)
img = Image.open(picName).resize((w, h))
img = ImageTk.PhotoImage(img)
canvas.create_image(w // 2, h // 2, image=img, anchor=CENTER)
tk.after(int(lag), update_ip)
# canvas.delete("all")
except StopIteration:
picNameIterator = (x for x in picNames)
update_ip()
update_ip()
tk.attributes('-topmost', True) # make canvas always on top window
tk.mainloop()