1

I'd like a to make a scrolled Tkinter textbox that fills the maximum alloted space. I have it working kind of...

For some reason when I stretch the window the text widget is fine; However, the scroll bar gets a ton of padding on the x axis.

The second problem is when I shrink the window the scrollbar goes of the screen.

Anyone know the solutions to these two programs?

snippet:

    self.Fr = Tkinter.Frame(self, width=self.Wi, height=self.He)
    self.Fr.pack(side='right', fill='both', expand='yes')


    self.Te = Tkinter.Text(self.Fr, font=self.Fo, fg=self.FG, bg=self.BG,
                           selectforeground=self.SFG,
                           selectbackground=self.SBG,
                           insertbackground=self.IBG, wrap='word',
                           undo=True, maxundo=100)
    #self.Te.grid(column=0, row=0, sticky='NSEW')
    self.Te.pack(side='left', fill='both', expand='yes')


    self.Sc = Tkinter.Scrollbar(self.Fr, elementborderwidth=1)
    #self.Sc.grid(column=1, row=0, sticky='NSEW')
    self.Sc.pack(side='right', fill='both', expand='yes')

    self.Te.configure(yscrollcommand=self.Sc.set)
    self.Sc.configure(command=self.Te.yview)
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

1 Answers1

7

Your scrollbar gets all the padding because you use fill='both'. Even though it's a vertical scrollbar you asked it to take up extra space along the x axis, which results in the padding since the scrollbar itself won't stretch to make a wide scrollbar. You want vertical scrollbars to only fill in the Y direction and horizontal ones to fill in the X direction.

As to the scrollbar going off screen, that's a little complex to explain but it has a simple solution.

The problem is this: if you shrink a window managed by pack to a point where it's smaller than that required by the widgets inside, it starts clipping widgets. The way this works is it processes the widgets in order, laying out the window and then allocating any left-over space for any remaining widgets. This means that if a widget early in the order takes up all the remaining visual space, any later widgets will not appear.

The "order" mentioned above is the order of the packing list. Specifically, the order in which items were packed. So, if you pack the text widget and then the scrollbar Tk will first lay out the text widget, and any remaining space will be allocated to the scrollbar. IF you had packed the scrollbar first, it would get laid out and any remaining space would be given to the text widget.

It all sounds very complex, but the cool thing is that if you pack things in the proper order it all just works.

The general rule of thumb, then, is to make sure the last widget you pack is the one with expand set to true. This is your "elastic" widget. That way all the fixed-size widgets will take up whatever space they need first, and your "elastic" widget will take up all that is left.

There is another solution which is to give your text widget a requested width and height of one. With that, when the packer initially allocates space it will allocate only a small amount of space. Thus, when the window shrinks the text widget will shrink until it gets down to that tiny size. This isn't very practical though, since one of the great features of pack is that you can give all widgets their natural size (or they assume their natural size based on their content in the case of buttons and labels) and the packer does all the work. If you set the width and height to one, your initial window (unless explicitly set to a larger size) will be rather small.

This behavior is all documented in the man page for pack, though you have to read it really closely to fully grasp this behavior.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Wow, that's some really good info. I had mainly worked with the .grid and .place methods. Therefor I had only a vague understanding of how the .pack method worked. This certainly cleared up my misunderstandings. – rectangletangle Dec 13 '10 at 23:20