Search code examples
pythontkinterttk

Sub Frame Is Not Expanded When Its Parent Is


import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

left_frame_1 = tk.Frame(root, background="#ff0000")
left_frame_1.grid(row=0, column=0)

left_frame_2 = tk.Frame(left_frame_1)
left_frame_2.grid(row=0, column=0)

left_label_1 = tk.Label(left_frame_2, text="HELLO")
left_label_2 = tk.Label(left_frame_2, text="WORLD")
left_label_3 = tk.Label(left_frame_2, text="=D")

left_label_1.grid(row=0, column=0)
left_label_2.grid(row=1, column=0)
left_label_3.grid(row=2, column=0)

right_frame1 = tk.Frame(root, background="#00ff00")
right_frame1.grid(row=0, column=1, sticky="nsew")

right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
right_frame_2.grid(row=0, column=0)

right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
right_label_1.grid(row=0, column=0)

root.mainloop()

When my parent frame expands to all its free space, the child frame doesn't, instead it just stays on top.

enter image description here


I've been testing if .grid() has something to do with it, but haven't found anything.

Even if I add sticky="nsew" to both the frame and the label, there is still no change.

right_frame1 = tk.Frame(root, background="#00ff00")
right_frame1.grid(row=0, column=1, sticky="nsew")

right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
right_frame_2.grid(row=0, column=0, sticky="nsew")

right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
right_label_1.grid(row=0, column=0, sticky="nsew")

My goal is for the parent frame (the one with the green color) to expand to all available space (which I've achieved), and for the child frame containing the label to expand.

enter image description here

right_frame_2 looks because it expands.

right_frame_1 is not visible because it is completely covered by right_frame_2.

I hope your help, thank you.


Solution

  • To get the result of the last image in the question, you need to:

    • change sticky options of .grid() for right_frame_2 and right_label_1
    • set weight options of .rowconfigure() and .columnconfigure() on root, right_frame1 and right_frame_2
    import tkinter as tk
    import tkinter.ttk as ttk
    
    root = tk.Tk()
    
    left_frame_1 = tk.Frame(root, background="#ff0000")
    left_frame_1.grid(row=0, column=0)
    
    left_frame_2 = tk.Frame(left_frame_1)
    left_frame_2.grid(row=0, column=0)
    
    left_label_1 = tk.Label(left_frame_2, text="HELLO")
    left_label_2 = tk.Label(left_frame_2, text="WORLD")
    left_label_3 = tk.Label(left_frame_2, text="=D")
    
    left_label_1.grid(row=0, column=0)
    left_label_2.grid(row=1, column=0)
    left_label_3.grid(row=2, column=0)
    
    right_frame1 = tk.Frame(root, background="#00ff00")
    right_frame1.grid(row=0, column=1, sticky="nsew")
    
    right_frame_2 = tk.Frame(right_frame1, background="#0000ff")
    right_frame_2.grid(row=0, column=0, sticky="nsew") # expand to fill available space
    
    right_label_1 = tk.Label(right_frame_2, text="CENTER ME!")
    right_label_1.grid(row=0, column=0, sticky="ew") # expand horizontally
    
    root.rowconfigure(0, weight=1) # make left and right frame expand vertically
    root.columnconfigure(1, weight=1) # make right frame expand horizontally
    
    # allocate all space to right_frame_2
    right_frame1.rowconfigure(0, weight=1)
    right_frame1.columnconfigure(0, weight=1)
    
    # allocate all space of right_frame_2 to right_label_1
    right_frame_2.rowconfigure(0, weight=1)
    right_frame_2.columnconfigure(0, weight=1)
    
    root.mainloop()
    

    Result:

    enter image description here

    When the window is resized:

    enter image description here