2009-09-03 25 views
0

我創建了一個框架對象,我想限制它可以展開的寬度。框架中唯一的窗口是一個ScrolledWindow對象,並且包含所有其他的子對象。我有很多對象與一個垂直方向的BoxSizer排列在一起,所以ScrolledWindow對象變得很高。通常在右側有一個滾動條,所以你可以上下滾動。在wxPython中使用ScrolledWindow設置框架的最大寬度

當我嘗試爲框架設置最大尺寸時,問題就出現了。我正在使用ScrolledWindow的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()沒有考慮滾動條的寬度。

+0

我很感興趣,但不知道問題或答案。你可以發佈一個小例子來說明這一點嗎? – tom10 2009-09-04 15:43:49

+0

感謝您發佈代碼。我看到了這個問題,並且玩了一下,但沒有弄明白。當然有人在wxPython用戶或wx用戶會知道。 (有一件事對我很好奇,雖然你在添加滾動條之前設置了所有尺寸。) – tom10 2009-09-07 21:08:19

+0

爲什麼設置最大尺寸?讓最終用戶使您的應用程序變得非常大有什麼危害? – 2010-01-07 19:15:35

回答

1

這有點難看,但似乎可以在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