pythontkinterresizeautoresize

how would i make 4 frames in each corner take up the same space when the window is resized?


I want to make a window with 5 frames, 4 in a 2x2 grid, and one below them. how would i have them take up the same space, even when the window is resized?

Im having trouble making the frames expand to the center and take up equal space. im also unsure how to place a 5th frame under everything. here is the code so far.

import tkinter as tk

window = tk.Tk()
window.geometry("500x500")

frame1 = tk.Frame(window,bg = 'red')
frame1.place(relx = 0, rely = 0, anchor = 'nw')
label1 = tk.Label(frame1, text = 'quadrant 2')
label1.grid(padx = 10, pady = 10)

frame2 = tk.Frame(window,bg = 'blue')
frame2.place(relx = 1, rely = 0, anchor = 'ne')
label2 = tk.Label(frame2, text = 'quadrant 1')
label2.grid(padx = 10, pady = 10)

frame3 = tk.Frame(window,bg = 'yellow')
frame3.place(relx = 0, rely = 1, anchor = 'sw')
label3 = tk.Label(frame3, text = 'quadrant 3')
label3.grid(padx = 10, pady = 10)

frame4 = tk.Frame(window,bg = 'green')
frame4.place(relx = 1, rely = 1, anchor = 'se')
label4 = tk.Label(frame4, text = 'quadrant 4')
label4.grid(padx = 10, pady = 10)

window.mainloop()

my code so far

Thanks for any help.

i tried putting frames in each corner and using fill and expand but i dont understand it very well. i also tried using grid and pack to place frames in each corner.

when i used grid to place frames in the corners, quadrant 1 was in row 0, column 1, and quadrant 2 was in row 0, column 0, quadrant 2 still appeared lower though despite being in the same row.

pack has me confused. how does it pack? idk. i tried using anchor commands and those worked alright.


Solution

  • You can put them in rows 0 and 1, and columns 0 and 1, using grid. You can then give each of those rows and columns a weight so that unused space is allocated to them,

    It would look something like this:

    f1 = tk.Frame(root, …)
    f2 = tk.Frame(root, …)
    f3 = tk.Frame(root, …)
    f4 = tk.Frame(root, …)
    
    f1.grid(row=0, column=0, sticky=“nsew”)
    f1.grid(row=0, column=1, sticky=“nsew”)
    f1.grid(row=1, column=0, sticky=“nsew”)
    f1.grid(row=1, column=1, sticky=“nsew”)
    
    root.grid_rowconfigure((0,1), weight=1)
    root.grid_columnconfigure((0,1), weight=1)