Search code examples
pythontkinterlabelframe

Tkinter Label won't appear inside Frame


I have two frames: big_frame and small_frame. small_frame is inside of big_frame, and I want to place a label inside of small_frame.

The label won't appear inside the frame and the problem is on line 8: removing the sticky allows the label to appear inside the frame. Am I doing something wrong, is there a work-around that still allows the frame to resize as the window is resized?

from tkinter import *

root = Tk()
root.geometry('700x500')

# Big Frame
big_frame = LabelFrame(root, text='Big Frame', width=350, height=450, padx=5, pady=5)
big_frame.grid(row=0, column=0, padx=(1, 0), sticky='nsew')  # the sticky prevents the label from being inside the frame

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

# Small Frame
small_frame = LabelFrame(big_frame, text='Small Frame', width=120, height=200).grid(row=0, column=0)

label = Label(small_frame, text='Label')
label.grid(row=0, column=0)


mainloop()

Solution

  • a small fix gridding smallframe after initializing

    from tkinter import *
    
    root = Tk()
    root.geometry('700x500')
    
    # Big Frame
    big_frame = LabelFrame(root, text='Big Frame', width=350, height=450, padx=5, pady=5)
    big_frame.grid(row=0, column=0, padx=(1, 0), sticky='nsew')  # the sticky prevents the label from being inside the frame
    
    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)
    
    # Small Frame
    small_frame = LabelFrame(big_frame, text='Small Frame', width=120, height=200)
    small_frame.grid(row=0, column=0)
    
    label = Label(small_frame, text='Label')
    label.grid(row=0, sticky='nw')
    
    
    mainloop()