Skip to content
Advertisement

python pack() and grid() methods together

Im new to python so please forgive my Noob-ness. Im trying to create a status bar at the bottom of my app window, but it seems every time I use the pack() and grid() methods together in the same file, the main app window doesn’t open. When I comment out the line that says statusbar.pack(side = BOTTOM, fill = X) my app window opens up fine but if I leave it in it doesn’t, and also if I comment out any lines that use the grid method the window opens with the status bar. It seems like I can only use either pack() or grid() but not both. I know I should be able to use both methods. Any suggestions? Here’s the code:

from Tkinter import *
import tkMessageBox

def Quit():
 answer = tkMessageBox.askokcancel('Quit', 'Are you sure?')
 if answer:
    app.destroy()

app = Tk()
app.geometry('700x500+400+200')
app.title('Title')

label_1 = Label(text = "Enter number")
label_1.grid(row = 0, column = 0)
text_box1 = DoubleVar() 
input1 = Entry(app, textvariable = text_box1)
input1.grid(row = 0, column = 2)

statusbar = Label(app, text = "", bd = 1, relief = SUNKEN, anchor = W)
statusbar.pack(side = BOTTOM, fill = X)

startButton = Button(app, text = "Start", command = StoreValues).grid(row = 9, column = 2,  padx = 15, pady = 15)

app.mainloop() 

Any help is appreciated! Thanks!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement