Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_slideshow.py
- import tkinter as tk
- import time
- from tkinter import filedialog
- from PIL import Image, ImageTk
- import os
- import random
- import ctypes
- # Define ctypes structures and functions
- class POINT(ctypes.Structure):
- _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
- def get_xy():
- point = POINT()
- ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
- return point.x, point.y
- def gui_update(event):
- global x, y, ww, hh
- # get the root position and height
- if not fullscreen:
- x = root.winfo_x()
- y = root.winfo_y()
- ww = root.winfo_width()
- hh = root.winfo_height()
- def make_fullscreen():
- global fullscreen
- fullscreen = True
- slideshow_button.place_forget()
- root.overrideredirect(True)
- w = screenwidth
- h = screenheight
- canvas.config(width=w, height=h)
- fullscreen_title_bar()
- start_slideshow()
- def default_screen():
- global fullscreen
- fullscreen = False
- show_buttons()
- root.overrideredirect(False)
- root.geometry(f"{x}x{y}+{ww}+{hh}")
- canvas.config(width=ww, height=hh)
- # Define functions
- def select_folder():
- global img_list, file_path, index
- # Prompt user to select a folder
- file_path = filedialog.askdirectory()
- if file_path:
- t = []
- # Load all images from the selected folder
- for image_file in os.listdir(file_path):
- if any(image_file.endswith(ext) for ext in ('bmp', 'webp', 'jfif', 'png', 'jpeg', 'jpg')):
- t.append(Image.open(os.path.join(file_path, image_file)))
- if t:
- # Display the first image in the list
- img_list = t[:]
- index = 0
- display_image()
- def shuffle_slideshow():
- global index
- random.shuffle(img_list)
- index = 0
- # Display the current image
- display_image()
- def delete_image():
- # ask "Confirm Deletion?"
- # delete the image if confirmed
- pass
- def display_image():
- # Update the image displayed on the canvas
- img = img_list[index]
- # resize the image if too large
- if img.size[0] > ww or img.size[1] > hh:
- # calculate the aspect ratio of the image and the canvas
- img_ratio = img.size[0] / img.size[1]
- canvas_ratio = ww / (hh)
- # calculate the new size based on aspect ratios
- if img_ratio > canvas_ratio:
- new_size = (ww, int(ww / img_ratio))
- else:
- new_size = (int(hh * img_ratio), hh)
- img = img.resize(new_size)
- photo = ImageTk.PhotoImage(img)
- label_img.config(image=photo)
- label_img.image = photo # avoid garbage collection
- def fullscreen_title_bar():
- global title_bar
- x, y = get_xy()
- if y < 90:
- if not title_bar:
- title_bar = 1
- root.overrideredirect(False)
- root.geometry("%dx%d+-8+0" % (ww, hh-72))
- show_buttons()
- else:
- if title_bar:
- title_bar = 0
- root.overrideredirect(True)
- root.geometry("%dx%d+-0+0" % (ww, hh))
- hide_buttons()
- def start_slideshow():
- global index, paused, title_bar
- hide_buttons()
- title_bar = 0
- paused = 0
- while True:
- fullscreen_title_bar()
- if not paused:
- # Increment the index to display the next image
- index += 1
- # If we reach the end of the list, wrap around to the beginning
- if index >= len(img_list):
- index = 0
- # Display the current image
- display_image()
- time.sleep(wait)
- canvas.update() # prevents freezing
- def hide_buttons():
- start_button.place_forget()
- select_button.place_forget()
- delete_button.place_forget()
- shuffle_button.place_forget()
- def show_buttons():
- xx = 5
- yp = 5
- def px():
- nonlocal xx
- xx = xx + 100
- return xx
- select_button.place(x=xx, y=yp)
- start_button.place(x=px(), y=yp)
- shuffle_button.place(x=px(), y=yp)
- delete_button.place(x=ww-100, y=yp)
- def prev_image():
- global index, paused
- paused = 1
- index -= 1
- if index < 0:
- index = len(img_list) - 1
- display_image()
- def next_image():
- global index, paused
- paused = 1
- index += 1
- if index >= len(img_list):
- index = 0
- display_image()
- def pause_slideshow():
- global paused
- paused = not paused
- # Create the main window
- root = tk.Tk()
- # Define global variables
- title_bar = 1
- img_list = [] # list to store all images in selected folder
- index = 0 # index to keep track of currently displayed image
- wait = 3 # time to wait before changing to the next image
- file_path = "" # path of selected folder
- fullscreen = False # whether or not the slideshow is currently in fullscreen mode
- paused = False # whether or not the slideshow is currently paused
- x = -9
- y = 0
- screenwidth = root.winfo_screenwidth()
- screenheight = root.winfo_screenheight()
- ww = screenwidth
- hh = screenheight
- root.geometry(f"{ww}x{hh}+{x}+{y}")
- # Create the canvas to display images on
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack(expand=True, fill=tk.BOTH)
- # Create the buttons to control the slideshow
- select_button = tk.Button(root, text="Select Folder", width=12, command=select_folder)
- delete_button = tk.Button(root, text="Delete", width=12, command=delete_image)
- shuffle_button = tk.Button(root, text="Shuffle", width=12, command=shuffle_slideshow)
- start_button = tk.Button(root, text="Start Slideshow", width=12, command=start_slideshow)
- show_buttons()
- # Create the label to hold the currently displayed image
- label_img = tk.Label(canvas, bg='black')
- label_img.pack(expand=True, fill=tk.BOTH)
- root.bind("<Left>", lambda event: prev_image())
- root.bind("<Right>", lambda event: next_image())
- root.bind("<space>", lambda event: pause_slideshow())
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement