0

我从一个关于 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()

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

4

1 回答 1

0

我通过添加 testpanel 在其中添加 scrolledPanel 解决了这个问题。当从不调用 onAdd() 时,在销毁所有 testpanel 的孩子之后,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()
于 2013-04-24T15:32:31.143 回答