From 8f0751170385989677392f806762a211f99412ef Mon Sep 17 00:00:00 2001 From: Vikas Kushwaha Date: Thu, 21 Nov 2024 13:23:10 +0530 Subject: First Commit --- test/fixratio.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/fixratio.py (limited to 'test/fixratio.py') diff --git a/test/fixratio.py b/test/fixratio.py new file mode 100644 index 0000000..a9393cd --- /dev/null +++ b/test/fixratio.py @@ -0,0 +1,33 @@ +import tkinter as tk +WIDTH, HEIGHT = 400, 300 # Defines aspect ratio of window. + +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. + +root = tk.Tk() +root.geometry(f'{WIDTH}x{HEIGHT}') +root.bind('', lambda event: maintain_aspect_ratio(event, WIDTH/HEIGHT)) +root.mainloop() -- cgit v1.2.3