Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from PIL import ImageTk, Image
- import os
- # Initialize the window
- window = tk.Tk()
- window.title("Image Gallery")
- # Directory containing the images
- image_directory = "path_to_image_directory"
- # Get the list of image files in the directory
- image_files = [file for file in os.listdir(image_directory) if file.endswith((".jpg", ".jpeg", ".png"))]
- # Initialize variables
- current_image = 0
- # Function to display the current image
- def display_image():
- global current_image
- # Load the current image file
- image_path = os.path.join(image_directory, image_files[current_image])
- image = Image.open(image_path)
- image.thumbnail((500, 500))
- photo = ImageTk.PhotoImage(image)
- # Update the image label
- image_label.configure(image=photo)
- image_label.image = photo
- # Function to move to the previous image
- def prev_image():
- global current_image
- if current_image > 0:
- current_image -= 1
- display_image()
- # Function to move to the next image
- def next_image():
- global current_image
- if current_image < len(image_files) - 1:
- current_image += 1
- display_image()
- # Create a label to display the image
- image_label = tk.Label(window)
- image_label.pack()
- # Create buttons for navigation
- prev_button = tk.Button(window, text="Prev", command=prev_image)
- prev_button.pack(side=tk.LEFT, padx=10)
- next_button = tk.Button(window, text="Next", command=next_image)
- next_button.pack(side=tk.RIGHT, padx=10)
- # Display the initial image
- display_image()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement