2013-04-24 49 views
0

我從Stackoverflow主題獲得此代碼ScrolledPanel inside Panel not sizing。這對我來說很有用。但是我想銷燬所有的scrolled_pa​​nel的子項,然後重新創建它的新子項。所以我修改這樣的代碼:ScrolledPanel在DestroyChildren之後沒有調整大小

import wx 
import wx.lib.scrolledpanel as scrolled 

######################################################################## 
class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(200,500)) 

     self.n = 13 
     # Add a panel so it looks the correct on all platforms 
     self.panel = wx.Panel(self, wx.ID_ANY) 

     # -------------------- 
     # Scrolled panel stuff 
     self.scrolled_panel = scrolled.ScrolledPanel(self.panel, -1, 
           style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel1") 
     self.scrolled_panel.SetAutoLayout(1) 
     self.scrolled_panel.SetupScrolling() 

     words = "A Quick Brown Insane Fox Jumped Over the Fence and Ziplined to Cover".split() 
     self.spSizer = wx.BoxSizer(wx.VERTICAL) 
     for word in words: 
      text = wx.TextCtrl(self.scrolled_panel, value=word) 
      self.spSizer.Add(text) 
     self.scrolled_panel.SetSizer(self.spSizer) 
     # -------------------- 

     btn = wx.Button(self.panel, label="Add Widget") 
     btn.Bind(wx.EVT_BUTTON, self.onAdd) 

     panelSizer = wx.BoxSizer(wx.VERTICAL) 
     panelSizer.AddSpacer(50) 
     panelSizer.Add(self.scrolled_panel, 1, wx.EXPAND) 
     panelSizer.Add(btn) 
     self.panel.SetSizer(panelSizer) 

    #---------------------------------------------------------------------- 
    def onAdd(self, event): 
     """""" 
     print "in onAdd" 
     self.n += 1 
     self.scrolled_panel.DestroyChildren() 
     for i in range(self.n): 
      new_text = wx.TextCtrl(self.scrolled_panel, value="New Text %s" % i) 
      self.spSizer.Add(new_text) 
     #new_text = wx.TextCtrl(self.scrolled_panel, value="New Text") 
     #self.spSizer.Add(new_text) 
     self.scrolled_panel.Layout() 
     self.scrolled_panel.SetupScrolling() 


# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm().Show() 
    app.MainLoop() 

現在,甚至當我創造更多的比面板的尺寸可以顯示孩子,我沒有看到滾動條的原代碼。誰能幫我這個?謝謝!

回答

0

我解決了這個問題,通過添加testpanel在其中添加scrolledPanel。當onAdd()是調用時,在銷燬所有testpanel的子元素之後,testpanel下的所有東西甚至是sizer都必須重新創建並重新設置。

我試過這樣做沒有testpanel,我仍然可以通過使用我的鼠標滾動,但沒有看到滾動條,我不知道爲什麼。這是我的新代碼:

import wx 
import wx.lib.scrolledpanel as scrolled 


######################################################################## 
class MyForm(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(200,500)) 
     self.n =17 
     # Add a panel so it looks the correct on all platforms 
     self.panel = wx.Panel(self, wx.ID_ANY) 
     panelSizer = wx.BoxSizer(wx.VERTICAL) 
     panelSizer.AddSpacer(50) 
     # add a panel 
     self.testpanel = wx.Panel(self.panel, wx.ID_ANY) 

     #self.testpanel.SetSizer(self.testpanelSizer) 
     panelSizer.Add(self.testpanel, 1, wx.EXPAND) 

     self.onAdd() 
     btn = wx.Button(self.panel, label="Add Widget") 
     btn.Bind(wx.EVT_BUTTON, self.onAdd) 
     panelSizer.Add(btn) 


     self.panel.SetSizer(panelSizer) 

    #---------------------------------------------------------------------- 
    def onAdd(self, event=None): 
     self.n +=1 
     self.testpanel.DestroyChildren() 
     testpanelSizer = wx.BoxSizer(wx.VERTICAL) 
     scrolled_panel = scrolled.ScrolledPanel(self.testpanel, -1, 
           style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel1") 
     scrolled_panel.SetAutoLayout(1) 
     scrolled_panel.SetupScrolling() 

     spSizer = wx.BoxSizer(wx.VERTICAL) 
     for i in range(self.n): 
      new_text = wx.TextCtrl(scrolled_panel, value="New Text %s" % i) 
      spSizer.Add(new_text) 
     scrolled_panel.SetSizer(spSizer) 

     testpanelSizer.Add(scrolled_panel, 1, wx.EXPAND) 
     self.testpanel.SetSizer(testpanelSizer) 
     self.testpanel.Layout() 


# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm().Show() 
    app.MainLoop()