Advertisement
here2share

# tk_slideshow.py

Mar 28th, 2023
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.46 KB | None | 0 0
  1. # tk_slideshow.py
  2.  
  3. import tkinter as tk
  4. import time
  5. from tkinter import filedialog
  6. from PIL import Image, ImageTk
  7. import os
  8. import random
  9. import ctypes
  10.  
  11. # Define ctypes structures and functions
  12. class POINT(ctypes.Structure):
  13.     _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
  14.  
  15. def get_xy():
  16.     point = POINT()
  17.     ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
  18.     return point.x, point.y
  19.  
  20. def gui_update(event):
  21.     global x, y, ww, hh
  22.     # get the root position and height
  23.     if not fullscreen:
  24.         x = root.winfo_x()
  25.         y = root.winfo_y()
  26.         ww = root.winfo_width()
  27.         hh = root.winfo_height()
  28.  
  29. def make_fullscreen():
  30.     global fullscreen
  31.     fullscreen = True
  32.     slideshow_button.place_forget()
  33.     root.overrideredirect(True)
  34.     w = screenwidth
  35.     h = screenheight
  36.     canvas.config(width=w, height=h)
  37.     fullscreen_title_bar()
  38.     start_slideshow()
  39.  
  40. def default_screen():
  41.     global fullscreen
  42.     fullscreen = False
  43.     show_buttons()
  44.     root.overrideredirect(False)
  45.     root.geometry(f"{x}x{y}+{ww}+{hh}")
  46.     canvas.config(width=ww, height=hh)
  47.  
  48. # Define functions
  49. def select_folder():
  50.     global img_list, file_path, index
  51.    
  52.     # Prompt user to select a folder
  53.     file_path = filedialog.askdirectory()
  54.    
  55.     if file_path:
  56.         t = []
  57.        
  58.         # Load all images from the selected folder
  59.         for image_file in os.listdir(file_path):
  60.             if any(image_file.endswith(ext) for ext in ('bmp', 'webp', 'jfif', 'png', 'jpeg', 'jpg')):
  61.                 t.append(Image.open(os.path.join(file_path, image_file)))
  62.  
  63.         if t:
  64.             # Display the first image in the list
  65.             img_list = t[:]
  66.             index = 0
  67.             display_image()
  68.  
  69. def shuffle_slideshow():
  70.     global index
  71.     random.shuffle(img_list)
  72.     index = 0
  73.        
  74.     # Display the current image
  75.     display_image()
  76.  
  77. def delete_image():
  78.     # ask "Confirm Deletion?"
  79.     # delete the image if confirmed
  80.     pass
  81.  
  82. def display_image():
  83.     # Update the image displayed on the canvas
  84.     img = img_list[index]
  85.        
  86.     # resize the image if too large
  87.     if img.size[0] > ww or img.size[1] > hh:
  88.         # calculate the aspect ratio of the image and the canvas
  89.         img_ratio = img.size[0] / img.size[1]
  90.         canvas_ratio = ww / (hh)
  91.  
  92.         # calculate the new size based on aspect ratios
  93.         if img_ratio > canvas_ratio:
  94.             new_size = (ww, int(ww / img_ratio))
  95.         else:
  96.             new_size = (int(hh * img_ratio), hh)
  97.         img = img.resize(new_size)
  98.     photo = ImageTk.PhotoImage(img)
  99.     label_img.config(image=photo)
  100.     label_img.image = photo    # avoid garbage collection
  101.  
  102. def fullscreen_title_bar():
  103.     global title_bar
  104.     x, y = get_xy()
  105.     if y < 90:
  106.         if not title_bar:
  107.             title_bar = 1
  108.             root.overrideredirect(False)
  109.             root.geometry("%dx%d+-8+0" % (ww, hh-72))
  110.             show_buttons()
  111.     else:
  112.         if title_bar:
  113.             title_bar = 0
  114.             root.overrideredirect(True)
  115.             root.geometry("%dx%d+-0+0" % (ww, hh))
  116.             hide_buttons()
  117.  
  118. def start_slideshow():
  119.     global index, paused, title_bar
  120.     hide_buttons()
  121.     title_bar = 0
  122.     paused = 0
  123.     while True:
  124.         fullscreen_title_bar()
  125.         if not paused:
  126.             # Increment the index to display the next image
  127.             index += 1
  128.            
  129.             # If we reach the end of the list, wrap around to the beginning
  130.             if index >= len(img_list):
  131.                 index = 0
  132.            
  133.             # Display the current image
  134.             display_image()
  135.            
  136.             time.sleep(wait)
  137.         canvas.update() # prevents freezing
  138.  
  139. def hide_buttons():
  140.     start_button.place_forget()
  141.     select_button.place_forget()
  142.     delete_button.place_forget()
  143.     shuffle_button.place_forget()
  144.  
  145. def show_buttons():
  146.     xx = 5
  147.     yp = 5
  148.     def px():
  149.         nonlocal xx
  150.         xx = xx + 100
  151.         return xx
  152.        
  153.     select_button.place(x=xx, y=yp)
  154.     start_button.place(x=px(), y=yp)
  155.     shuffle_button.place(x=px(), y=yp)
  156.     delete_button.place(x=ww-100, y=yp)
  157.  
  158. def prev_image():
  159.     global index, paused
  160.     paused = 1
  161.     index -= 1
  162.     if index < 0:
  163.         index = len(img_list) - 1
  164.     display_image()
  165.  
  166. def next_image():
  167.     global index, paused
  168.     paused = 1
  169.     index += 1
  170.     if index >= len(img_list):
  171.         index = 0
  172.     display_image()
  173.    
  174. def pause_slideshow():
  175.     global paused
  176.     paused = not paused
  177.  
  178. # Create the main window
  179. root = tk.Tk()
  180.  
  181. # Define global variables
  182. title_bar = 1
  183. img_list = []               # list to store all images in selected folder
  184. index = 0                   # index to keep track of currently displayed image
  185. wait = 3                    # time to wait before changing to the next image
  186. file_path = ""              # path of selected folder
  187. fullscreen = False          # whether or not the slideshow is currently in fullscreen mode
  188. paused = False              # whether or not the slideshow is currently paused
  189. x = -9
  190. y = 0
  191. screenwidth = root.winfo_screenwidth()
  192. screenheight = root.winfo_screenheight()
  193. ww = screenwidth
  194. hh = screenheight
  195.  
  196. root.geometry(f"{ww}x{hh}+{x}+{y}")
  197.  
  198. # Create the canvas to display images on
  199. canvas = tk.Canvas(root, width=ww, height=hh)
  200. canvas.pack(expand=True, fill=tk.BOTH)
  201.  
  202. # Create the buttons to control the slideshow
  203. select_button = tk.Button(root, text="Select Folder", width=12, command=select_folder)
  204. delete_button = tk.Button(root, text="Delete", width=12, command=delete_image)
  205. shuffle_button = tk.Button(root, text="Shuffle", width=12, command=shuffle_slideshow)
  206. start_button = tk.Button(root, text="Start Slideshow", width=12, command=start_slideshow)
  207. show_buttons()
  208.  
  209. # Create the label to hold the currently displayed image
  210. label_img = tk.Label(canvas, bg='black')
  211. label_img.pack(expand=True, fill=tk.BOTH)
  212.  
  213. root.bind("<Left>", lambda event: prev_image())
  214. root.bind("<Right>", lambda event: next_image())
  215. root.bind("<space>", lambda event: pause_slideshow())
  216.  
  217. root.mainloop()
  218.  
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement