diff --git a/tk.py b/tk.py index 886ec02..6383d1c 100644 --- a/tk.py +++ b/tk.py @@ -61,11 +61,38 @@ def set_timer_in_seconds(user_data): 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("200x200") + image_window.geometry("1280x1024") random.shuffle(found_images) image = found_images[0] found_images.pop(0) @@ -77,6 +104,7 @@ def display_new_image(image_window, 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