[英]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.