from tkinter import * from tkinter import filedialog from pathlib import Path from functools import partial from PIL import ImageTk, Image import random root = Tk() root.title("Drawing Training!") root.geometry("300x600") selected_folder = "" found_images = [] timer = 0 def format_seconds(seconds): hours = None minutes = seconds / 60 if minutes >= 1: if minutes >= 60: hours = int(minutes / 60) minutes = int(minutes % 60) else: minutes = int(minutes) seconds = seconds % 60 else: minutes = None time = "" if hours is not None and hours != 0: time += str(hours) + "h" if minutes is not None and minutes != 0: time += str(minutes) + "m" if seconds is not None and seconds != 0: time += str(seconds) + "s" return time def check_lets_draw(): global selected_folder global found_images global timer if selected_folder != "" and len(found_images) > 0 and timer != 0: launch_button.config(state="normal") else: launch_button.config(state="disabled") def set_timer_in_seconds(user_data): global timer timer = user_data print(timer) for button in buttons: if button['text'] == format_seconds(timer): button.config(bg="blue") else: button.config(bg="SystemButtonFace") check_lets_draw() def maintain_aspect_ratio(event, aspect_ratio): """ Event handler to override root window resize events to maintain the specified width to height aspect ratio. """ # if event.widget.master: # Not root window? # return # Ignore. # events contain the widget's new width and height in pixels. new_aspect_ratio = event.width / event.height # Decide which dimension controls. if new_aspect_ratio > aspect_ratio: # Use width as the controlling dimension. desired_width = event.width desired_height = int(event.width / aspect_ratio) else: # Use height as the controlling dimension. desired_height = event.height desired_width = int(event.height * aspect_ratio) # Override if necessary. if event.width != desired_width or event.height != desired_height: # Manually give it the proper dimensions. event.widget.geometry(f'{desired_width}x{desired_height}') return "break" # Block further processing of this event. def lets_draw(): global found_images image_window = Toplevel(root) image_window.title("Image") image_window.geometry("1280x1024") random.shuffle(found_images) image = found_images[0] found_images.pop(0) display_new_image(image_window, image) def display_new_image(image_window, image): image1 = Image.open(image) test = ImageTk.PhotoImage(image1) label1 = Label(image_window, image=test) label1.bind('', lambda event: maintain_aspect_ratio(event, 1280 / 1024)) label1.image = test # Position image label1.place(x=0, y=0) def select_folder(): global selected_folder global found_images global folder_name global images_len selected_folder = filedialog.askdirectory() found_images = list(p.resolve() for p in Path(selected_folder).glob("**/*") if p.suffix in {".jpg", ".gif", ".png"}) folder_name.config(text="Folder : " + selected_folder) images_len.config(text="Found : " + str(len(found_images))) check_lets_draw() title = Label(root, text="Drawing Training") title.pack() folder_selector = Button(root, text="Select a folder", command=select_folder) folder_selector.pack() folder_name = Label(root, text="Folder : " + selected_folder) folder_name.pack() images_len = Label(root, text="Found : " + str(len(found_images))) images_len.pack() button_frame = Frame(root) button_frame.pack(fill=X) timers = [30, 45, 60, 120, 300, 600] buttons = [] i = 0 for i in timers: t = i new_button = Button(button_frame, text=format_seconds(t), command=partial(set_timer_in_seconds, t)) # new_button.pack(side="left") buttons.append(new_button) button_frame.columnconfigure(i, weight=1) new_button.grid(row=0, column=i, sticky=W + E) i += 1 launch_button = Button(root, text="Let's draw!", command=lets_draw, state="disabled") launch_button.pack(side="bottom") root.mainloop()