79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
import random
|
|
from tkinter import *
|
|
from PIL import ImageTk, Image
|
|
|
|
from util import Util
|
|
|
|
|
|
class ImageWindow:
|
|
def __init__(self, window):
|
|
self.window = Toplevel(window)
|
|
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
|
|
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=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.display_new_image()
|
|
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('<Configure>', 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()
|
|
|
|
# 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)
|
|
|
|
image_to_display = ImageTk.PhotoImage(self.current_image)
|
|
self.image_label.configure(image=image_to_display)
|
|
self.image_label.image = image_to_display
|