简体   繁体   English

在wxPython中使用ScrolledWindow设置帧的最大宽度

[英]Set Max Width for Frame with ScrolledWindow in wxPython

I created a Frame object and I want to limit the width it can expand to. 我创建了一个Frame对象,我想限制它可以扩展到的宽度。 The only window in the frame is a ScrolledWindow object and that contains all other children. 框架中唯一的窗口是ScrolledWindow对象,其中包含所有其他子级。 I have a lot of objects arranged with a BoxSizer oriented vertically so the ScrolledWindow object gets pretty tall. 我有很多对象都与BoxSizer垂直放置,因此ScrolledWindow对象变得很高。 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. 我正在使用ScrolledWindow的scrolled_window.GetBestSize scrolled_window.GetBestSize() (或scrolled_window.GetEffectiveMinSize() )函数,但它们没有考虑垂直滚动条。 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. 如果创建此框架,则会看到self.SetMaxSize设置得太窄。 There will always be a horizontal scrollbar since self.scroll.GetBestSize() didn't account for the width of scrollbar. 因为self.scroll.GetBestSize()不考虑滚动条的宽度,所以总会有一个水平滚动条。

This is a little ugly, but seems to work on Window and Linux. 这有点难看,但是似乎可以在Window和Linux上使用。 There is difference, though. 不过有区别。 The self.GetVirtualSize() seems to return different values on each platform. self.GetVirtualSize()似乎在每个平台上返回不同的值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM