ScrolledPanel在DestroyChildren之后没有调整大小

时间:2013-04-24 14:12:13

标签: resize scroll wxpython panel destroy

我从Stackoverflow ScrolledPanel inside Panel not sizing上的主题获得了此代码。这对我来说很有用。但是我想要销毁scrolled_pa​​nel的所有子项,然后重新创建它的新子项。所以我修改了这样的代码:

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()

现在,即使我创建的子项超出了面板大小可以显示的范围,我也看不到滚动条作为原始代码。谁能帮我这个?谢谢!

1 个答案:

答案 0 :(得分:0)

我通过添加testpanel来添加scrolledPanel来解决这个问题。当调用onAdd()时,在销毁所有testpanel的子节点之后,必须重新创建并重新设置测试面板甚至sizer下的所有内容。

我试图在没有测试面板的情况下这样做,我仍然可以使用我的鼠标滚动,但没有看到滚动条,我不知道为什么。这是我的新代码:

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()