python pack() and grid() methods together python pack() and grid() methods together tkinter tkinter

python pack() and grid() methods together


You cannot use both pack and grid on widgets that have the same master. The first one will adjust the size of the widget. The other will see the change, and resize everything to fit it's own constraints. The first will see these changes and resize everything again to fit its constraints. The other will see the changes, and so on ad infinitum. They will be stuck in an eternal struggle for supremacy.

While it is technically possible if you really, really know what you're doing, for all intents and purposes you can't mix them in the same container. You can mix them all you want in your app as a whole, but for a given container (typically, a frame), you can use only one to manage the direct contents of the container.

A very common technique is to divide your GUI into pieces. In your case you have a bottom statusbar, and a top "main" area. So, pack the statusbar along the bottom and create a frame that you pack above it for the main part of the GUI. Then, everything else has the main frame as its parent, and inside that frame you can use grid or pack or whatever you want.


Yeah thats right. In following example, i have divided my program into 2 frames. frame1 caters towards menu/toolbar and uses pack() methods wherein frame2 is used to make login page credentials and uses grid() methods.

from tkinter import *def donothing():    print ('IT WORKED')root=Tk()root.title(string='LOGIN PAGE')frame1=Frame(root)frame1.pack(side=TOP,fill=X)frame2=Frame(root)frame2.pack(side=TOP, fill=X)m=Menu(frame1)root.config(menu=m)submenu=Menu(m)m.add_cascade(label='File',menu=submenu)submenu.add_command(label='New File', command=donothing)submenu.add_command(label='Open', command=donothing)submenu.add_separator()submenu.add_command(label='Exit', command=frame1.quit)editmenu=Menu(m)m.add_cascade(label='Edit', menu=editmenu)editmenu.add_command(label='Cut',command=donothing)editmenu.add_command(label='Copy',command=donothing)editmenu.add_command(label='Paste',command=donothing)editmenu.add_separator()editmenu.add_command(label='Exit', command=frame1.quit)# **** ToolBar *******toolbar=Frame(frame1,bg='grey')toolbar.pack(side=TOP,fill=X)btn1=Button(toolbar, text='Print', command=donothing)btn2=Button(toolbar, text='Paste', command=donothing)btn3=Button(toolbar, text='Cut', command=donothing)btn4=Button(toolbar, text='Copy', command=donothing)btn1.pack(side=LEFT,padx=2)btn2.pack(side=LEFT,padx=2)btn3.pack(side=LEFT,padx=2)btn4.pack(side=LEFT,padx=2)# ***** LOGIN CREDENTIALS ******label=Label(frame2,text='WELCOME TO MY PAGE',fg='red',bg='white')label.grid(row=3,column=1)label1=Label(frame2,text='Name')label2=Label(frame2,text='Password')label1.grid(row=4,column=0,sticky=E)label2.grid(row=5,column=0,sticky=E)entry1=Entry(frame2)entry2=Entry(frame2)entry1.grid(row=4,column=1)entry2.grid(row=5,column=1)chk=Checkbutton(frame2,text='KEEP ME LOGGED IN')chk.grid(row=6,column=1)btn=Button(frame2,text='SUBMIT')btn.grid(row=7,column=1)# **** StatusBar ******************status= Label(root,text='Loading',bd=1,relief=SUNKEN,anchor=W)status.pack(side=BOTTOM, fill=X)