From 53c77d960ad40602639301b9f4e46a7aa51160e1 Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Mon, 22 May 2023 22:27:12 +0200 Subject: [PATCH] :sparkles: Make the image slider and timer work! --- app.py | 6 ++-- image.py | 95 ++++++++++++++++++++++++++------------------------------ 2 files changed, 48 insertions(+), 53 deletions(-) diff --git a/app.py b/app.py index bc7c1db..3912c61 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,8 @@ from image import ImageWindow class App: def __init__(self, root): super().__init__() + self.image_window = None + self.root = root self.selected_folder = "" self.found_images = [] self.timer = 0 @@ -42,10 +44,10 @@ class App: self.launch_button = Button(root, text="Let's draw!", command=self.lets_draw, state="disabled") self.launch_button.pack(side="bottom") - self.image_window = ImageWindow(root) def lets_draw(self): - self.image_window.lets_draw(self.found_images, self.timer) + self.image_window = ImageWindow(self.root) + self.image_window.lets_draw(self.found_images.copy(), self.timer) def select_folder(self): self.selected_folder = filedialog.askdirectory() diff --git a/image.py b/image.py index ac23f61..640c1ba 100644 --- a/image.py +++ b/image.py @@ -2,6 +2,8 @@ import random from tkinter import * from PIL import ImageTk, Image +from util import Util + class ImageWindow: def __init__(self, window): @@ -9,77 +11,68 @@ class ImageWindow: self.window.title("Image") self.window.geometry("1280x1024") self.images = [] + self.current_image = None + self.image_label = None self.timer = 0 self.timer_label = None self.timer_check = None - - @staticmethod - def maintain_aspect_ratio(event, aspect_ratio): - new_width = event.width - new_height = event.height - - new_aspect_ratio = new_width / new_height - - # Decide which dimension controls. - if new_aspect_ratio > aspect_ratio: - # Use width as the controlling dimension. - desired_width = new_width - desired_height = int(new_width / aspect_ratio) - else: - # Use height as the controlling dimension. - desired_height = new_height - desired_width = int(new_height * aspect_ratio) - - event.widget.resize(f'{desired_width}x{desired_height}') - # Override if necessary. - # if new_width != desired_width or new_height != desired_height: - # # Manually give it the proper dimensions. - # return "break" # Block further processing of this event. + self.image_label = Label(self.window) + self.image_label.pack(side="top", fill="both", expand=1) + self.timer_label = Label(self.window, text=self.timer) + self.timer_label.pack(side=BOTTOM) def update_timer(self, current): current -= 1 if current > 0: - self.timer_label.configure(text=current) + self.timer_label.configure(text=Util.format_seconds(current)) self.timer_check = self.window.after(1000, self.update_timer, current) else: self.window.after_cancel(self.timer_check) + if len(self.images) == 0: + self.window.destroy() + self.window.update() + + return + self.display_new_image() + self.display_new_timer() def lets_draw(self, images, timer): + random.shuffle(images) self.images = images self.timer = timer - canvas = Canvas(self.window) - canvas.pack(fill=BOTH, expand=1) - - self.timer_label = Label(self.window, text="") - self.timer_label.pack(side=BOTTOM) - - random.shuffle(self.images) - - image_timer = timer + # canvas = Canvas(self.window) + # canvas.pack(fill=BOTH, expand=1) self.display_new_image() - timer_check = self.window.after(1000, self.update_timer, image_timer) + self.display_new_timer() + + def display_new_timer(self): + self.timer_label.configure(text=self.timer) + self.timer_check = self.window.after(1000, self.update_timer, self.timer) def display_new_image(self): + image_path = self.images[0] + self.images.pop(0) + self.current_image = Image.open(image_path) + + image_to_display = ImageTk.PhotoImage(self.current_image) + + self.image_label.configure(image=image_to_display) + self.image_label.bind('', lambda event: self.resize_image()) + self.image_label.image = image_to_display + + def resize_image(self): ma = self.window.winfo_toplevel() w, h = ma.winfo_width(), ma.winfo_height() - image_path = self.images[0] - self.images.pop(0) - image = Image.open(image_path) + # if self.current_image.size[0] > w or self.current_image.size[1] > h: + if w < 21 or h < 21: # too small + w = 1280 + h = 1024 + self.current_image.thumbnail((w - 20, h - 20), Image.ANTIALIAS) + print("resized: win %s >= img %s", (w, h), self.current_image.size) - if image.size[0] > w or image.size[1] > h: - if w < 3 or h < 3: # too small - return # do nothing - image.thumbnail((w - 2, h - 2), Image.ANTIALIAS) - print("resized: win %s >= img %s", (w, h), image.size) - - image_to_display = ImageTk.PhotoImage(image) - - image_label = Label(self.window, image=image_to_display) - # image_label.bind('', lambda event: self.maintain_aspect_ratio(event, 1280 / 1024)) - image_label.image = image_to_display - image_label.pack(side="top", fill="both", expand=1) - # Position image - # image_label.place(x=0, y=0) + image_to_display = ImageTk.PhotoImage(self.current_image) + self.image_label.configure(image=image_to_display) + self.image_label.image = image_to_display