[Tutor] My first program doesn't seem to do anything -- followup
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Apr 28 20:51:04 EDT 2024
On 29/04/2024 00:37, Alan Gauld via Tutor wrote:
> Here is an almost minimal version that works. I did add a couple of
> extras for usability and debugging's sake.
Oops sorry, That was an earlier debugging version.
Here is the finished version:
################
import os
import time
import tkinter as tk
from PIL import Image, ImageTk
# Get a list of all .jpg files in the folder
folder_path = "c:/Users/Tim Carr/Pictures/_Parents"
image_files = [f for f in os.listdir(folder_path)
if f.endswith((".jpg",".jpeg",".JPG",".JPEG"))]
index = 0
thePhoto = thePicture = None
def show_image(interval=3000):
global index, thePhoto, thePicture
index += 1
thePicture.destroy()
del(thePhoto)
# Display next image for 3 seconds
if index < len(image_files):
file_name = image_files[index]
image_path = os.path.join(folder_path, file_name)
caption['text'] = file_name
# create and display a new image
newImage = Image.open(image_path)
thePhoto = ImageTk.PhotoImage(newImage)
thePicture = tk.Label(window, image = thePhoto,
width=newImage.width)
window['width'] = 500
thePicture.pack()
# get ready for the next update event
window.after(interval, lambda t=interval: show_image(t))
else: # we're finished
window.quit()
# Create a Tkinter window
window = tk.Tk()
window.title("Image Viewer")
caption = tk.Label(window, text=image_files[0])
caption.pack()
# show the first image
theImage = Image.open(os.path.join(folder_path,image_files[0]))
thePhoto = ImageTk.PhotoImage(theImage)
thePicture = tk.Label(window, image = thePhoto)
thePicture.pack()
#start the slide show
show_image()
window.mainloop()
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list