🚧 Try to make the image fill the window

This commit is contained in:
Shikiryu 2023-05-22 01:25:29 +02:00
parent f820851db4
commit 3047c5dad4

30
tk.py
View File

@ -61,11 +61,38 @@ def set_timer_in_seconds(user_data):
check_lets_draw() 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.
# <Configure> 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(): def lets_draw():
global found_images global found_images
image_window = Toplevel(root) image_window = Toplevel(root)
image_window.title("Image") image_window.title("Image")
image_window.geometry("200x200") image_window.geometry("1280x1024")
random.shuffle(found_images) random.shuffle(found_images)
image = found_images[0] image = found_images[0]
found_images.pop(0) found_images.pop(0)
@ -77,6 +104,7 @@ def display_new_image(image_window, image):
test = ImageTk.PhotoImage(image1) test = ImageTk.PhotoImage(image1)
label1 = Label(image_window, image=test) label1 = Label(image_window, image=test)
label1.bind('<Configure>', lambda event: maintain_aspect_ratio(event, 1280 / 1024))
label1.image = test label1.image = test
# Position image # Position image