[wxpython-users] How to create a GUI that changes layout dynamically?

Anton Blomberg anton.blomberg at swiss.se
Thu Apr 3 06:29:23 PDT 2008


Hi,
I'm trying to create a GUI with a lot of different controls, which
should all be minimizeable. So that when one control minimizes the other
controls in the layout can resize to use the freed space.

Does anyone know how to do this in wxPython, or just point me in the
right direction? Are there any programs that does this that I can look
at? Any pages in the wiki that could be useful?
I've been looking all around.

The appropriate wx-controls I found are wxFoldPanelBar and
wxCollapsiblePane. FoldPanelBar did minimize, but couldn't resize to
acquire the cleared space. CollapsiblePane worked but resized the whole
frame upon Collapse/Expand if not major workarounds were used.

This is my last try with CollapsiblePane:
------------------

I've created a simple GUI with two CollapsiblePanes on top of each other
in a vertical BoxSizer. When I minimize one pane I want all other
panes to resize and increase height to cover the newly aquired space.

I got this result with the code example below. But with that program,
the user can't resize the frame. It will pop back to its original,
hardcoded, size when a CollapsiblePane is collapsed/expanded.
Does anyone know a way around this problem?

If I don't force the frame to a specified size it will resize totally
and behave strange. Look at ## (1) ## and ## (2) ## in this example:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent =3D None, size =3D (500,500))
        self.frameZ =3D frameZ =3D wx.BoxSizer(wx.VERTICAL) # Create main s=
izer
       =

        ## (1) ## If this SetMinSize() is omitted, the frame will resize
totally
        frameZ.SetMinSize((500,500)) # Lock the size of the frame
        self.SetSizer(frameZ)
       =

        # Create CollapsePane wrapper objects, pass frame as arg
        cp1 =3D ColPaneWrapper(self)
        cp2 =3D ColPaneWrapper(self)
       =

        # Do Layout() and SetSizeHints() for main frame
        frameZ.Layout()
        frameZ.SetSizeHints(self)

class ColPaneWrapper:
    def __init__(self, frame):
        self.cp =3D cp =3D wx.CollapsiblePane(frame) # Create CollapsePane
object, frame as parent
        self.frameZ =3D frameZ =3D frame.frameZ # Get frame sizer
        self.frame =3D frame # Get frame
        frame.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnCollapse, cp)
# Bind OnCollapse event
       =

        cp_pane =3D cp.GetPane() # Get pane to use as parent for controls
and sizer
        self.cpZ =3D cpZ =3D wx.BoxSizer(wx.VERTICAL) # Create CP sizer
        cp_pane.SetSizer(cpZ) # Assign sizer to CPs pane
       =

        button =3D wx.Button(cp_pane, label =3D 'SomePlaceholderControl') #
Create button, use CPs pane as parent
        cpZ.Add(button, 1, wx.EXPAND | wx.ALL, 0) # Add button to CP sizer
        frameZ.Add(cp, 1, wx.EXPAND | wx.ALL, 0) # Add CP to frame sizer
       =

        # Do OnCollapse to refresh the state of the panes
        self.OnCollapse()

    def OnCollapse(self, evt =3D None):
    # OnCollapse:
    # Change the Sizer-Proportion-state of the CP to match the
Expanded/Collapsed state
        if self.cp.IsCollapsed():
            self.frameZ.GetItem(self.cp).SetProportion(0)
        else:
            self.frameZ.GetItem(self.cp).SetProportion(1)
       =

        # Do Layout() for CPs sizer
        self.cpZ.Layout()
       =

        ## (2) ## If this SetSizeHints() is omitted, the frame will
resize totally.
        self.frameZ.SetSizeHints(self.frame) # SetSizeHints for frame sizer

if __name__ =3D=3D "__main__":
    app =3D wx.App()
    MainFrame().Show()
    app.MainLoop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200804=
03/524f9684/attachment-0001.htm


More information about the wxpython-users mailing list