[wxPython-users] Best practice to rganize the code

Mark Erbaugh mark at microenh.com
Sat Mar 15 20:36:03 PDT 2008


On Sat, 2008-03-15 at 15:08 +0000, Norbert Klamann wrote:
> Hello List,
> i build a mildly complex wxPython App for the presentation of Loans. The
> application has the following structure
> 
> Frame
> +- ButtonPanel
> +- Menu
> +- Splitterwindow
>    +- leftpanel
>    |    several Input Controls with labels
>    +- rightpanel
>       +- Notebook
>          +- overviewpanel
>          |    several Output Controls and labels
>          +- gridpanel
>          |    grid with numbers
>          +- plotpanel
>               matplotlib plot
>       +- errorpanel
>           several texts. This panel is visikle 
>             instead of the Notebook if something goes wrong
> 
> The whole thing works in principle but the code is a mess. I have it organized
> as the ASCII-Art above shows. There is a function 'make_rightpanel' and a
> function 'make_plotpanel' and so on. 
> 
> This in itself is no problem but these functions are hard to read. They define
> controls, they define sizers and place the controls in the sizers. 
> 
> So in essence these functions define the contents of 
> the frame and the layout structure in one step and this smells.
> 
> Here is a short example(yagut.* arhe thin wrappers around the wx-CVontrols):
> -------
> def make_errorsizer(parent):
>     sizer = yagut.BoxSizer(parent,name='error',orient=wx.VERTICAL)
>     s1 = wx.BoxSizer(wx.HORIZONTAL)
>     sizer.Add(yagut.Label(parent,-1,name = "error_label" ))
>     sizer.Add((60, 20), 0, wx.EXPAND)
>     s1.Add(yagut.Label(parent,-1,name = "error_text" ),wx.EXPAND)
>     sizer.Add(s1)
>     s2 = wx.BoxSizer(wx.HORIZONTAL)
>     s2.Add(yagut.Label(parent,-1,name = "error_help" ),wx.EXPAND)
>     sizer.Add(s2)
>     return sizer
> 
> -------
> 
> 
> In an ideal world one would define all controls in one step as a flat list of
> objects and then define the hierarchical sizer structure in a second step and
> place the controls in this structure.
> 
> Problem is: Notebooks, Splitters and friends introduce a hierarchy 
> of Panels and the controls must be placed in panels to display properly.
> 
> So we have 2 parallel hierarchies - one of panels and one of sizers and this 
> speaks trouble.
> 
> How do people handle this ?
> 

I don't create my controls in one place.

I to organize my displays around Panels. Starting at the lowest level
you will have a panel that does not contain any sub-panels. I will
create that panel in a module. The class in that module will be a
wx.Panel descendent and will create all the controls and place them
within the panel by using sizers. That class will have a SetValue and a
GetValue method which are responsible for taking the values that need to
be displayed and returning the values from the controls.

As you move up the display, that panel class will be placed using sizers
on a higher level panel.  The higher level panel's SetValue routine
calls the lower level panel's SetValue routine and it's GetValue routine
calls the GetValue routine.

So basically, each Panel's SetValue method is responsible for taking the
associated data and displaying it, and that includes passing sub-data to
a sub-panel's SetValue method.

Mark






More information about the wxpython-users mailing list