[Tutor] moving from pack to grid in tkinter
john at fouhy.net
Mon Nov 27 06:07:33 CET 2006
On 27/11/06, Tony Cappellini <cappy2112 at gmail.com> wrote:
> I've got a main window which is 640 * 400.
> self.label.pack(side=BOTTOM, fill=X)
> This line would put the label at the bottom of the window, and extend to
> both left and right edges of the window.
So, something like a status bar?
Using pack() is really the easiest way to do this. Here's what I'd do:
self.label = Label(root) # etc
self.interior = Frame(root)
self.interior.pack(SIDE=TOP, fill=BOTH, expand=True)
...and then create all your other widgets as children of self.interior.
> I want to change to grid geometry because I have several more widgets to put
> in the main window.
> self.label.grid(row=5, column=2, column=0)
Not sure why you have two column= options here.. One of them should be
> This put the label at row 5, in the center of the window, but the label
> didn't extend to the window edges.
Using grid geometry, widgets will take their natural size and sit in
the middle of the cell you place them in. If you want to override
this, you need to specify which edge or edges you want the widget to
In this case, you want the widget to stick to both the left and right
edges, so that it is stretched across the width of the screen. You
can do this by saying:
self.label.grid(row=5, column=0, columnspan=2, sticky=E+W)
E+W means "east and west".
> self.label.grid(row=10, column=2, column=0) didn't move the label any lower
> than row 5
If there is nothing in a row, the row will have a height of zero. So
row 5 and row 10 are equivalent, if rows 6..9 are empty and there are
no other widgets in 5 or 10.
There's no way to say "put this in the last row", but you could put it
in row 999 or something.
> Although, the main window isn't calling grid explicitly either- that's a bit
> confusing, because I thought all widgets had to call grid or pack to be
The root window Tk(), and any windows you create using Toplevel(),
will display themselves. Other widgets need to be put inside a frame
and pack()ed or grid()ded (or place()d).
More information about the Tutor