blob: a9393cd888b950945fc970d4c8431226951b223d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.
# <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.
root = tk.Tk()
root.geometry(f'{WIDTH}x{HEIGHT}')
root.bind('<Configure>', lambda event: maintain_aspect_ratio(event, WIDTH/HEIGHT))
root.mainloop()
|