Search code examples
pythonuser-interfacetkinterframe

How to add an Frame inside another frame in Tkinter?


I am trying to add a frame inside the another frame using tkinter, but the frame is not added:

Code:

lframe=Frame(r,width=1000,height=650,relief='raise',bd=8).pack(side=LEFT)

rframe=Frame(r,width=350,height=650,relief='raise',bd=8).pack(side=RIGHT)



lframe1=Frame(lframe,width=1000,height=100,bd=8,relief='raise').pack(side= TOP)

lframe2=Frame(lframe,width=1000,height=55,bd=8,relief='raise').pack(side=TOP)

rframe1=Frame(rframe,width=350,height=215,relief='raise',bd=8).pack(side=TOP)

rframe2=Frame(rframe,width=350,height=415,relief='raise',bd=8).pack(side=TOP)

r.mainloop()

its shows the following output

Output


Solution

  • What caused your problem?

    lframe is worth None and does not refer to the frame instance you created in anyway. Why so?

    Because pack(), gird() and place() layout managers are functions that return nothing. So when you write:

    lframe=Frame(r,width=1000,height=650,relief='raise',bd=8).pack(side=LEFT)
    

    You are clearly getting None from pack(side=LEFT)

    How to fix your problem?

    Simply follow this principle whenever you create a widget: create then position (In your case, you create and position the widget on the same time). I personally refer to this principle as The SaYa idiom. This means the previous line of code should be written as follows:

    lframe=Frame(r,width=1000,height=650,relief='raise',bd=8)
    lframe.pack(side=LEFT)
    

    Apply this idiom to all your widgets and that will save you from some troubles.