Re: My first program doesn't seem to do anything -- followup
Alan Gauld via Tutor <[email protected]> Mon, 29 Apr 2024 00:37:27 +0100
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
On 28/04/2024 16:14, [email protected] wrote: > > I asked an AI to write a python program that would scroll through a folder > of pictures, and to display each .jpg it finds for 15 seconds. You need to remember that ChatGPT and its friends are very good at stitching stuff from the web together in such a way that it looks like it was done by a human. But that's all. They make no promises about the stuff being accurate or correct in any meaningful sense. And as you may have discovered they are extremely poor at debugging. > import os > import time > import tkinter as tk > from PIL import Image, ImageTk > > folder_path = "c:/Users/Tim Carr/Pictures/_Parents" > > def display_images(folder_path): > > # Create a Tkinter window > window = tk.Tk() > window.title("Image Viewer") > > # Get a list of all .jpg files in the folder > image_files = [f for f in os.listdir(folder_path) if f.endswith(".jpg")] > > # Display each image for 15 seconds > for image_file in image_files: > image_path = os.path.join(folder_path, image_file) > image = Image.open(image_path) > photo = ImageTk.PhotoImage(image) > > # Create a label to display the image > label = tk.Label(window, image=photo) > label.pack() > > # Display the image for 15 seconds > window.after(15000, label.destroy) > > window.mainloop() Here is an almost minimal version that works. I did add a couple of extras for usability and debugging's sake. It's quite a bit different... 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 def show_image(interval=3000): global index, thePhoto, thePicture 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'] = image_path # 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)) index += 1 else: # we're finished window.quit() # Create a Tkinter window window = tk.Tk() window.title("Image Viewer") caption = tk.Label(window, text=image_files[-1].lower()) caption.pack() theImage = Image.open(os.path.join(folder_path,image_files[-1])) thePhoto = ImageTk.PhotoImage(theImage) thePicture = tk.Label(window, image = thePhoto) thePicture.pack() 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 _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor