Advertisement
CodeCrusader

GUI Photo Previewer App

Jun 10th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | Software | 0 0
  1. import tkinter as tk
  2. from PIL import ImageTk, Image
  3. from tkinter import filedialog
  4.  
  5. # Initialize the window
  6. window = tk.Tk()
  7. window.title("Image Viewer")
  8.  
  9. # Function to open an image file
  10. def open_image():
  11.     file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.gif")])
  12.     if file_path:
  13.         image = Image.open(file_path)
  14.         image.thumbnail((400, 400))
  15.         photo = ImageTk.PhotoImage(image)
  16.         image_label.configure(image=photo)
  17.         image_label.image = photo
  18.  
  19. # Create a button to open an image file
  20. open_button = tk.Button(window, text="Open Image", command=open_image)
  21. open_button.pack()
  22.  
  23. # Create a label to display the image
  24. image_label = tk.Label(window)
  25. image_label.pack()
  26.  
  27. window.mainloop()
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement