♻️ Goes to class and first image displayed

This commit is contained in:
Clement Desmidt 2023-05-22 17:50:05 +02:00
parent 3047c5dad4
commit e2b51f8b00
4 changed files with 192 additions and 57 deletions

71
app.py Normal file
View File

@ -0,0 +1,71 @@
from tkinter import *
from tkinter import filedialog
from pathlib import Path
from util import Util
from functools import partial
from image import ImageWindow
class App:
def __init__(self, root):
super().__init__()
self.selected_folder = ""
self.found_images = []
self.timer = 0
self.title = Label(root, text="Drawing Training")
self.title.pack()
self.folder_selector = Button(root, text="Select a folder", command=self.select_folder)
self.folder_selector.pack()
self.folder_name = Label(root, text="Folder : " + self.selected_folder)
self.folder_name.pack()
self.images_len = Label(root, text="Found : " + str(len(self.found_images)))
self.images_len.pack()
self.button_frame = Frame(root)
self.button_frame.pack(fill=X)
timers = [30, 45, 60, 120, 300, 600]
self.buttons = []
i = 0
for i in timers:
t = i
new_button = Button(self.button_frame, text=Util.format_seconds(t),
command=partial(self.set_timer_in_seconds, t))
# new_button.pack(side="left")
self.buttons.append(new_button)
self.button_frame.columnconfigure(i, weight=1)
new_button.grid(row=0, column=i, sticky=W + E)
i += 1
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)
def select_folder(self):
self.selected_folder = filedialog.askdirectory()
self.found_images = list(
p.resolve() for p in Path(self.selected_folder).glob("**/*") if p.suffix in {".jpg", ".gif", ".png"})
self.folder_name.config(text="Folder : " + self.selected_folder)
self.images_len.config(text="Found : " + str(len(self.found_images)))
self.check_lets_draw()
def check_lets_draw(self):
if self.selected_folder != "" and len(self.found_images) > 0 and self.timer != 0:
self.launch_button.config(state="normal")
else:
self.launch_button.config(state="disabled")
def set_timer_in_seconds(self, user_data):
self.timer = user_data
for button in self.buttons:
if button['text'] == Util.format_seconds(self.timer):
button.config(bg="blue")
else:
button.config(bg="gray85")
self.check_lets_draw()

85
image.py Normal file
View File

@ -0,0 +1,85 @@
import random
from tkinter import *
from PIL import ImageTk, Image
class ImageWindow:
def __init__(self, window):
self.window = Toplevel(window)
self.window.title("Image")
self.window.geometry("1280x1024")
self.images = []
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.
def update_timer(self, current):
current -= 1
if current > 0:
self.timer_label.configure(text=current)
self.timer_check = self.window.after(1000, self.update_timer, current)
else:
self.window.after_cancel(self.timer_check)
def lets_draw(self, images, timer):
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
self.display_new_image()
timer_check = self.window.after(1000, self.update_timer, image_timer)
def display_new_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 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('<Configure>', 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)

69
main.py
View File

@ -1,61 +1,16 @@
import dearpygui.dearpygui as dpg from tkinter import *
from pathlib import Path from app import App
dpg.create_context()
selected_folder = ""
found_images = []
timer = 0
with dpg.theme() as blue_theme:
with dpg.theme_component(dpg.mvAll):
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (0, 0, 200), category=dpg.mvThemeCat_Core)
def setTimerInSeconds(sender, app_data, user_data): def main():
timer=user_data root = Tk()
print(sender) root.title("Drawing Training!")
print(dpg.get_item_theme(sender)) root.geometry("300x600")
dpg.bind_item_theme(sender, blue_theme)
print(dpg.get_item_theme(sender)) app = App(root)
root.mainloop()
def callback(sender, app_data): if __name__ == '__main__':
selected_folder = app_data['file_path_name'] main()
found_images = list(p.resolve() for p in Path(selected_folder).glob("**/*") if p.suffix in {".jpg", ".gif", ".png"})
dpg.set_value("folder_text", "Folder : " + selected_folder)
dpg.set_value("found_images_text", "Found : " + str(len(found_images)))
print('OK was clicked.')
print("Sender: ", sender)
print("App Data: ", app_data)
def cancel_callback(sender, app_data):
print('Cancel was clicked.')
print("Sender: ", sender)
print("App Data: ", app_data)
with dpg.file_dialog(directory_selector=True, show=False, callback=callback, tag="file_dialog_id",
cancel_callback=cancel_callback, width=700, height=400):
dpg.add_file_extension("Images (*.jpg *.gif *.png){.jpg,.gif,.png}", color=(0, 255, 255, 255))
with dpg.window(label="Drawing Training", width=300, height=800, pos=(0, 0), no_title_bar=True):
dpg.add_text("Drawing Training")
dpg.add_button(label="Select a folder", callback=lambda: dpg.show_item("file_dialog_id"))
dpg.add_text("Folder : " + selected_folder, tag="folder_text")
dpg.add_text("Found : " + str(len(found_images)), tag="found_images_text")
with dpg.group(horizontal=True):
dpg.add_button(label="30s", callback=setTimerInSeconds, user_data=30, tag="timer_30")
dpg.add_button(label="45s", callback=setTimerInSeconds, user_data=45, tag="timer_45")
dpg.add_button(label="1m", callback=setTimerInSeconds, user_data=1*60, tag="timer_60")
dpg.add_button(label="2m", callback=setTimerInSeconds, user_data=2*60, tag="timer_120")
dpg.add_button(label="5m", callback=setTimerInSeconds, user_data=5*60, tag="timer_300")
dpg.add_button(label="10m", callback=setTimerInSeconds, user_data=10*60, tag="timer_600")
dpg.add_button(label="Let's draw!")
dpg.create_viewport(title='Drawing Training', width=300, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

24
util.py Normal file
View File

@ -0,0 +1,24 @@
class Util:
@staticmethod
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