Tkinter,show pictures from the list of files in catalog-problem

MRAB python at mrabarnett.plus.com
Tue Dec 19 14:17:37 EST 2017


On 2017-12-19 18:33, Ziggy wrote:
> I have a problem with this code, it seems to work but.... first it shows
> the picture then supposed to iterate through file list and shows them
> each changed after 3seconds however show just the last one from list.
> https://paste.pound-python.org/show/txvB4IBtlUrn3TuB0rtu/
> 
The function called by .after should return to the tkinter's event loop. 
If you want to display a sequence of pictures, then the function should 
call .after to make it call the function again.

Here's a slightly reworked version:


import collections
import os

pictures = collections.deque()

for folder, subfolders, files in os.walk('/home/vimart/Python/img/'):
     for file in files:
         pictures.append(os.path.join(folder, file))

canvas_width = 300
canvas_height =300

master = Tk()

canvas = Canvas(master, width=canvas_width, height=canvas_height)
canvas.pack()

img = PhotoImage(file="sport.gif")
canvas.create_image(20,20, anchor=NW, image=img)

def change_img():
     canvas.delete("all")
     canvas.img = PhotoImage(file=pictures.popleft())
     canvas.create_image(20,20, anchor=NW, image=canvas.img)

     # If there's more to come, call again later.
     if pictures:
         master.after(3000, change_img)

master.after(3000, change_img)
mainloop()



More information about the Python-list mailing list