繁体   English   中英

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

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

我创建了一个Frame对象,我想限制它可以扩展到的宽度。 框架中唯一的窗口是ScrolledWindow对象,其中包含所有其他子级。 我有很多对象都与BoxSizer垂直放置,因此ScrolledWindow对象变得很高。 经常在右侧有一个滚动条,因此您可以上下滚动。

当我尝试为框架设置最大尺寸时,问题就来了。 我正在使用ScrolledWindow的scrolled_window.GetBestSize scrolled_window.GetBestSize() (或scrolled_window.GetEffectiveMinSize() )函数,但它们没有考虑垂直滚动条。 我最终得到的框架太窄了,并且有一个水平滚动条,它永远不会消失。

有没有一种方法可以补偿垂直滚动条的宽度? 如果没有,我如何获得滚动条的宽度,以便可以手动将其添加到框架的最大尺寸?

这是一个高而窄的框架的示例:

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

如果创建此框架,则会看到self.SetMaxSize设置得太窄。 因为self.scroll.GetBestSize()不考虑滚动条的宽度,所以总会有一个水平滚动条。

这有点难看,但是似乎可以在Window和Linux上使用。 不过有区别。 self.GetVirtualSize()似乎在每个平台上返回不同的值。 无论如何,我认为这可能对您有帮助。

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