tkinter grid() question

Greg Ewing greg.ewing at
Wed Nov 3 10:24:34 CET 1999

"Garrett G. Hodgson" wrote:
> in the code below, i create a Text entry area with a Scrollbar.
> i want this pair to always use all of the available space.
> but when i resize the window, the stay the same size, neither
> expanding or shrinking.

You have to do two things: make the Text widget sticky on
all sides, and configure the weight of the row and column
containing the Text widget so that it sucks up all the
extra space.

It's simplest if the widgets only take up one cell of
the grid each, so I'll show you how to do it that way:

   self.body.grid(row = 0, column = 0, sticky = N+S+E+W)
   scrollbar.grid(row = 0, column = 1, sticky = N+S)
   self.rowconfigure(0, weight = 1)
   self.columnconfigure(0, weight = 1)

Since you're not putting anything else in the frame in
the code you showed, there's really no reason to have
anything spanning more than one row or column. If you
want to control the initial size of the Text widget,
use the width and height options when you create it.

If you're going to put other widgets in the frame as
well, you'll have to be careful how you configure the 
grid so that resizing the text widget properly doesn't  
have undesirable side effects on the layout of the other

Essentially, you'll have to pick one of the columns spanned
by the text widget and configure it's weight to be 1, but
not one that has another widget in it that you don't want
stretched as well. Or, instead of trying to put everything
in one frame, you may find it easier to use more subframes
so that you can control their layouts independently.

I can't give any more specific advice without knowing more 
about what you're trying to do.

Hope that helps,

More information about the Python-list mailing list