Решение Брайана Окли не подходит, поскольку я хочу упаковать или сгруппировать больше виджетов в правом фрейме. Решение Гэри Керра. Я не могу заставить его работать.
Общая ширина окна составляет 700 пикселей, а высота — 200 пикселей (181 пиксель верхнего контейнера и 19 пикселей нижнего контейнера). Я хочу, чтобы левый (темно-серый) контейнер был размером 181x181, а правый (зеленый) контейнер 700 - 181 = 519 пикселей в ширину и 181 пиксель в высоту. Эти размеры являются лишь примером после изменения размера главного окна. После принудительного увеличения относительных весов с помощью columnsconfigure и расширения окна вправо левый контейнер продолжает расти и не является квадратом:

import tkinter as tk
from tkinter import ttk
from tkinter import Label
class App:
def __init__(self, root):
self.root = root
self.root.geometry("400x200+900+400")
# Styles
self.style = ttk.Style(self.root)
self.style.configure('Dark.TFrame', background='#343434')
self.style.configure('LightGreen.TFrame', background='#a4edde')
# Main frames: top (for content) and bottom (status bar)
self.top_container = ttk.Frame(self.root)
self.bottom_container = ttk.Frame(self.root)
self.top_container.pack(fill='both', expand=True)
self.bottom_container.pack(fill='x', expand=False)
# Left container for future Canvas map
self.left_container = ttk.Frame(self.top_container, style='Dark.TFrame')
# Right container for data display
self.right_container = ttk.Frame(self.top_container, style='LightGreen.TFrame')
self.top_container.rowconfigure(0, weight=1)
self.top_container.columnconfigure(0, weight=1)
self.top_container.columnconfigure(1, weight=2)
self.left_container.grid(row=0, column=0, sticky='NSWE')
self.right_container.grid(row=0, column=1, sticky='NSWE')
# Labels just for displaying sizes
self.left_label = ttk.Label(self.left_container, text="Left")
self.right_label = ttk.Label(self.right_container, text="Right")
self.bottom_label = ttk.Label(self.bottom_container, text="Bottom")
self.left_label.pack()
self.right_label.pack()
self.bottom_label.pack()
self.root.bind('', self.resize)
def resize(self, event):
# Remove the binding. It will be bound again later.
self.root.unbind('')
w, h = event.width, event.height
w1, h1 = self.root.winfo_width(), self.root.winfo_height()
self.root.update_idletasks()
W = self.top_container.winfo_width()
H = self.top_container.winfo_height()
lw = self.left_container.winfo_width()
lh = self.left_container.winfo_height()
rw = self.right_container.winfo_width()
rh = self.right_container.winfo_height()
self.bottom_label.config(text=f'{W=} {H=}')
self.left_label.config(text=f'{lw=} {lh=}')
self.right_label.config(text=f'{rw=} {rh=}')
print(f'SIZES: {W=} {H=} {w=} {h=} {lw=} {rw=} {lh=} {rh=}')
print('LEFT WEIGHT', self.top_container.columnconfigure(0)['weight'])
print('RIGHT WEIGHT', self.top_container.columnconfigure(1)['weight'])
if W > H:
if lw != H:
# self.left_container.grid_forget()
# self.right_container.grid_forget()
self.top_container.columnconfigure(0, weight=H)
self.top_container.columnconfigure(1, weight=W-H)
# self.left_container.grid(row=0, column=0, sticky='NSWE')
# self.right_container.grid(row=0, column=1, sticky='NSWE')
else:
self.top_container.columnconfigure(0, weight=1)
self.top_container.columnconfigure(1, weight=1)
elif H > W:
# TO DO
pass
# Enable binding again
self.root.bind('', self.resize)
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()
Подробнее здесь: https://stackoverflow.com/questions/798 ... e-a-square
Мобильная версия