简体   繁体   中英

allocating more size in sizer to wx.CollapsiblePane when expanded

I have several CollapsiblePanes in a vertical BoxSizer. I would like to be able to expand and collapse them without having them run into each other. I am running wxPython 2.8.10.1 on Windows 7.

Runnable sample application demonstrating the problem is below.

import wx

class SampleCollapsiblePane(wx.CollapsiblePane):
    def __init__(self, *args, **kwargs):
        wx.CollapsiblePane.__init__(self,*args,**kwargs)
        sizer = wx.BoxSizer(wx.VERTICAL)
        for x in range(5):
            sizer.Add(wx.Button(self.GetPane(), label = str(x)))
        self.GetPane().SetSizer(sizer)


class Main_Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.main_panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        for x in range(5):
            sizer.Add(SampleCollapsiblePane(self.main_panel, label = str(x)), 1)
        self.main_panel.SetSizer(sizer)


class SampleApp(wx.App):
    def OnInit(self):
        frame = Main_Frame(None, title = "Sample App")
        frame.Show(True)
        frame.Centre()
        return True

def main():
    app = SampleApp(0)
    app.MainLoop()

if __name__ == "__main__":
    main()

The documentation explicitly states that you should use proportion=0 when adding collapsible panes to a sizer.

http://docs.wxwidgets.org/stable/wx_wxcollapsiblepane.html

So, first, change the 1 at the end of this line to a 0:

sizer.Add(SampleCollapsiblePane(self.main_panel, label = str(x)), 1)

Next, add this to your SampleCollapsiblePane to force the parent frame to re-layout when a pane is collapsed or expanded:

def __init__(...):
    ...
    self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.on_change)
def on_change(self, event):
    self.GetParent().Layout()

There might be a better way, but this is what I've got working at the moment. I'm good with wxPython but haven't used CollapsiblePanes before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM