views:

265

answers:

1

I created a Frame object and I want to limit the width it can expand to. The only window in the frame is a ScrolledWindow object and that contains all other children. I have a lot of objects arranged with a BoxSizer oriented vertically so the ScrolledWindow object gets pretty tall. There is often a scrollbar to the right so you can scroll up and down.

The problem comes when I try to set a max size for the frame. I'm using the scrolled_window.GetBestSize() (or scrolled_window.GetEffectiveMinSize()) functions of ScrolledWindow, but they don't take into account the vertical scrollbar. I end up having a frame that's just a little too narrow and there's a horizontal scrollbar that will never go away.

Is there a method that will account compensate for the vertical scrollbar's width? If not, how would I get the scrollbar's width so I can manually add it to the my frame's max size?

Here's an example with a tall but narrow frame:

class TallFrame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, parent=None, title='Tall Frame')
    self.scroll = wx.ScrolledWindow(parent=self) # our scroll area where we'll put everything
    scroll_sizer = wx.BoxSizer(wx.VERTICAL)

    # Fill the scroll area with something...
    for i in xrange(10):
      textbox = wx.StaticText(self.scroll, -1, "%d) Some random text" % i, size=(400, 100))
      scroll_sizer.Add(textbox, 0, wx.EXPAND)
    self.scroll.SetSizer(scroll_sizer)
    self.scroll.Fit()

    width, height = self.scroll.GetBestSize()
    self.SetMaxSize((width, -1)) # Trying to limit the width of our frame
    self.scroll.SetScrollbars(1, 1, width, height) # throwing up some scrollbars

If you create this frame you'll see that self.SetMaxSize is set too narrow. There will always be a horizontal scrollbar since self.scroll.GetBestSize() didn't account for the width of scrollbar.

A: 

This is a little ugly, but seems to work on Window and Linux. There is difference, though. The self.GetVirtualSize() seems to return different values on each platform. At any rate, I think this may help you.

width, height = self.scroll.GetBestSize()
width_2, height_2 = self.GetVirtualSize()
print width
print width_2
dx = wx.SystemSettings_GetMetric(wx.SYS_VSCROLL_X)
print dx
self.SetMaxSize((width + (width - width_2) + dx, -1)) # Trying to limit the width of our frame
self.scroll.SetScrollbars(1, 1, width, height) # throwing up some scrollbars
NobodyReally