0

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.

3
  • I'm interested but don't know the problem or the answer. Could you post a small code example that illustrates the point? Commented Sep 4, 2009 at 15:43
  • Thanks for posting the code. I see the issue and played around with it a bit but didn't figure it out. Surely someone on the wxPython-users or wx-users will know. (One thing that's curious to me though it that you're setting all your sizes before you add the scrollbar.) Commented Sep 7, 2009 at 21:08
  • why set a max size? What's the harm in letting an end user make your app really large? Commented Jan 7, 2010 at 19:15

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.