Search code examples
pythonpython-2.7tkintertkinter-layout

Frame inside another frame In python Tkinter


I am trying to place frame inside a frame.... like i want to place buttoncontainer and buttoncontainer2 inside the bodycontainer frame. But when i do so bodycontainer vanished,.. i cant figure out my problem.. i know i am doing some silly mistake.... Please help me to figure out this problem.

from Tkinter import *
root = Tk()
root.title('Tkinter Apps')
root.resizable(width=TRUE, height=TRUE)
root.geometry('{}x{}'.format(800, 500))

headerbanner=Frame(root,width=790,height=50,highlightbackground="gray",highlightthickness=1)
headerbanner.grid(row=0,column=0,padx=5,pady=2)

bodycontainer=Frame(root,width=790,height=380,highlightbackground="gray",highlightthickness=1)
bodycontainer.grid(row=1,column=0,padx=5,pady=0)

buttoncontainer=Frame(bodycontainer,width=50,height=300,highlightbackground="gray",highlightthickness=1)
buttoncontainer.grid(row=0,column=0)
buttoncontainer2=Frame(bodycontainer,width=50,height=300,highlightbackground="gray",highlightthickness=1)
buttoncontainer2.grid(row=0,column=1)

root.mainloop()

code

Screenshot of the output


Solution

  • The grid() geometry manager will shrink a frame to fit the contained widgets. Therefore it looks like the frame has disappeared. You can stop this size propagation with grid_propagate(False). See my example below. I have given each frame a different color to show where they are:

    from Tkinter import *
    root = Tk()
    root.title('Tkinter Apps')
    root.resizable(width=TRUE, height=TRUE)
    root.geometry('{}x{}'.format(800, 500))
    
    headerbanner=Frame(root,width=790,height=50,bg='khaki')
    headerbanner.grid(row=0,column=0,padx=5,pady=2)
    
    bodycontainer=Frame(root,width=790,height=380,bg='tan')
    bodycontainer.grid(row=1,column=0,padx=5,pady=0)
    bodycontainer.grid_propagate(False) # Stop grid() from resizing bodycontainer
    
    buttoncontainer=Frame(bodycontainer,width=50,height=300,bg='powder blue')
    buttoncontainer.grid(row=0,column=0)
    buttoncontainer2=Frame(bodycontainer,width=50,height=300,bg='olivedrab1')
    buttoncontainer2.grid(row=0,column=1)
    
    root.mainloop()