2023-05-23 19:36:15 +02:00
|
|
|
import os
|
2023-05-22 17:50:05 +02:00
|
|
|
import random
|
|
|
|
from tkinter import *
|
2023-05-23 17:42:42 +02:00
|
|
|
from PIL import ImageTk, Image, ImageOps
|
2023-05-22 22:27:12 +02:00
|
|
|
from util import Util
|
|
|
|
|
2023-05-22 17:50:05 +02:00
|
|
|
|
|
|
|
class ImageWindow:
|
2023-05-23 19:36:15 +02:00
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
self.window = Toplevel(app.root)
|
2023-05-22 17:50:05 +02:00
|
|
|
self.window.title("Image")
|
|
|
|
self.window.geometry("1280x1024")
|
2023-05-23 17:42:42 +02:00
|
|
|
self.option = {
|
|
|
|
"bw": False,
|
2023-05-23 21:59:39 +02:00
|
|
|
"mirror": False,
|
|
|
|
"always_on_top": False,
|
2023-05-23 17:42:42 +02:00
|
|
|
}
|
2023-05-22 17:50:05 +02:00
|
|
|
self.images = []
|
2023-05-22 22:27:12 +02:00
|
|
|
self.current_image = None
|
|
|
|
self.image_label = None
|
2023-05-23 13:39:16 +02:00
|
|
|
toolbar = Frame(self.window, bd=1, relief=RAISED)
|
|
|
|
toolbar.pack(side=BOTTOM, fill=X)
|
2023-05-22 17:50:05 +02:00
|
|
|
self.timer = 0
|
|
|
|
self.timer_label = None
|
|
|
|
self.timer_check = None
|
2023-05-23 21:59:39 +02:00
|
|
|
self.timer_check = None
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-23 13:39:16 +02:00
|
|
|
next_button = Button(toolbar, relief=FLAT, compound=LEFT, text="next ➡️", command=self.next_image)
|
|
|
|
next_button.pack(side=LEFT, padx=0, pady=0)
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-23 19:27:48 +02:00
|
|
|
self.bw_button = Button(toolbar, relief=FLAT, compound=LEFT, text="black&white",
|
|
|
|
command=self.toggle_black_white)
|
|
|
|
self.bw_button.pack(side=LEFT, padx=0, pady=0)
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-23 19:27:48 +02:00
|
|
|
self.mirror_button = Button(toolbar, relief=FLAT, compound=LEFT, command=self.toggle_mirror, text="mirror")
|
|
|
|
self.mirror_button.pack(side=LEFT, padx=0, pady=0)
|
|
|
|
|
2023-05-23 19:36:15 +02:00
|
|
|
self.open_folder_button = Button(toolbar, relief=FLAT, compound=LEFT, command=self.open_folder
|
|
|
|
, text="open folder")
|
|
|
|
self.open_folder_button.pack(side=LEFT, padx=0, pady=0)
|
|
|
|
|
2023-05-23 21:59:39 +02:00
|
|
|
self.always_on_top_button = Button(toolbar, relief=FLAT, compound=LEFT, command=self.toggle_always_on_top
|
|
|
|
, text="always on top")
|
|
|
|
self.always_on_top_button.pack(side=LEFT, padx=0, pady=0)
|
|
|
|
self.always_on_top_check = None
|
|
|
|
|
2023-05-23 19:27:48 +02:00
|
|
|
self.timer_button = Button(toolbar, relief=FLAT, compound=LEFT, command=self.toggle_timer, text="timer")
|
|
|
|
self.timer_button.pack(side=LEFT, padx=0, pady=0)
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
self.image_label = Label(self.window)
|
2023-05-23 17:42:42 +02:00
|
|
|
self.image_label.pack(side=TOP, fill=BOTH, expand=1)
|
2023-05-23 19:27:48 +02:00
|
|
|
|
2023-05-23 13:39:16 +02:00
|
|
|
self.timer_label = Label(toolbar, text=self.timer)
|
2023-05-23 17:42:42 +02:00
|
|
|
self.timer_label.pack(side=RIGHT, ipadx=20)
|
2023-05-22 17:50:05 +02:00
|
|
|
|
|
|
|
def update_timer(self, current):
|
|
|
|
current -= 1
|
|
|
|
if current > 0:
|
2023-05-22 22:27:12 +02:00
|
|
|
self.timer_label.configure(text=Util.format_seconds(current))
|
2023-05-22 17:50:05 +02:00
|
|
|
self.timer_check = self.window.after(1000, self.update_timer, current)
|
|
|
|
else:
|
|
|
|
self.window.after_cancel(self.timer_check)
|
2023-05-22 22:27:12 +02:00
|
|
|
if len(self.images) == 0:
|
|
|
|
self.window.destroy()
|
|
|
|
self.window.update()
|
|
|
|
return
|
2023-05-23 13:39:16 +02:00
|
|
|
self.next_image()
|
|
|
|
|
|
|
|
def next_image(self):
|
|
|
|
if self.timer_check is not None:
|
|
|
|
self.window.after_cancel(self.timer_check)
|
|
|
|
self.display_new_image()
|
|
|
|
self.display_new_timer()
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-23 17:42:42 +02:00
|
|
|
def toggle_black_white(self):
|
|
|
|
self.option["bw"] = not self.option["bw"]
|
2023-05-23 19:27:48 +02:00
|
|
|
self.bw_button.config(bg="blue" if self.option["bw"] else "grey85")
|
2023-05-23 17:42:42 +02:00
|
|
|
|
|
|
|
def toggle_mirror(self):
|
|
|
|
self.option["mirror"] = not self.option["mirror"]
|
2023-05-23 19:27:48 +02:00
|
|
|
self.mirror_button.config(bg="blue" if self.option["mirror"] else "grey85")
|
|
|
|
|
|
|
|
def toggle_timer(self):
|
|
|
|
try:
|
|
|
|
self.timer_label.pack_info()
|
|
|
|
self.timer_label.pack_forget()
|
|
|
|
except TclError:
|
|
|
|
self.timer_label.pack(side=RIGHT, ipadx=20)
|
2023-05-23 19:36:15 +02:00
|
|
|
|
|
|
|
def open_folder(self):
|
|
|
|
os.startfile(self.app.selected_folder)
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-23 21:59:39 +02:00
|
|
|
def toggle_always_on_top(self):
|
|
|
|
self.option["always_on_top"] = not self.option["always_on_top"]
|
|
|
|
if self.option["always_on_top"]:
|
|
|
|
self.always_on_top_button.config(bg="blue")
|
|
|
|
self.stay_on_top()
|
|
|
|
else:
|
|
|
|
self.always_on_top_button.config(bg="grey85")
|
|
|
|
self.window.after_cancel(self.always_on_top_check)
|
|
|
|
|
|
|
|
def stay_on_top(self):
|
|
|
|
# self.window.lift()
|
|
|
|
self.window.attributes('-topmost', True)
|
|
|
|
self.always_on_top_check = self.window.after(2000, self.stay_on_top)
|
|
|
|
|
2023-05-22 17:50:05 +02:00
|
|
|
def lets_draw(self, images, timer):
|
2023-05-22 22:27:12 +02:00
|
|
|
random.shuffle(images)
|
2023-05-22 17:50:05 +02:00
|
|
|
self.images = images
|
|
|
|
self.timer = timer
|
2023-05-23 13:39:16 +02:00
|
|
|
self.next_image()
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
def display_new_timer(self):
|
|
|
|
self.timer_label.configure(text=self.timer)
|
|
|
|
self.timer_check = self.window.after(1000, self.update_timer, self.timer)
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
def display_new_image(self):
|
2023-05-22 17:50:05 +02:00
|
|
|
image_path = self.images[0]
|
|
|
|
self.images.pop(0)
|
2023-05-22 22:27:12 +02:00
|
|
|
self.current_image = Image.open(image_path)
|
|
|
|
|
2023-05-23 13:39:16 +02:00
|
|
|
self.resize_image()
|
|
|
|
|
2023-05-23 17:42:42 +02:00
|
|
|
if self.option["bw"]:
|
|
|
|
self.current_image = self.current_image.convert("L")
|
|
|
|
|
|
|
|
if self.option["mirror"]:
|
|
|
|
self.current_image = ImageOps.mirror(self.current_image)
|
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
image_to_display = ImageTk.PhotoImage(self.current_image)
|
|
|
|
|
|
|
|
self.image_label.configure(image=image_to_display)
|
|
|
|
self.image_label.bind('<Configure>', lambda event: self.resize_image())
|
|
|
|
self.image_label.image = image_to_display
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
def resize_image(self):
|
|
|
|
ma = self.window.winfo_toplevel()
|
|
|
|
w, h = ma.winfo_width(), ma.winfo_height()
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
# 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)
|
2023-05-23 17:42:42 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
print("resized: win %s >= img %s", (w, h), self.current_image.size)
|
2023-05-22 17:50:05 +02:00
|
|
|
|
2023-05-22 22:27:12 +02:00
|
|
|
image_to_display = ImageTk.PhotoImage(self.current_image)
|
|
|
|
self.image_label.configure(image=image_to_display)
|
|
|
|
self.image_label.image = image_to_display
|