[wxPython-users] Best practice to rganize the code

C M cmpython at gmail.com
Sat Mar 15 15:36:05 PDT 2008


On Sat, Mar 15, 2008 at 11:08 AM, Norbert Klamann <
Norbert.Klamann at projecthome.de> 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 =3D yagut.BoxSizer(parent,name=3D'error',orient=3Dwx.VERTICAL)
>    s1 =3D wx.BoxSizer(wx.HORIZONTAL)
>    sizer.Add(yagut.Label(parent,-1,name =3D "error_label" ))
>    sizer.Add((60, 20), 0, wx.EXPAND)
>    s1.Add(yagut.Label(parent,-1,name =3D "error_text" ),wx.EXPAND)
>    sizer.Add(s1)
>    s2 =3D wx.BoxSizer(wx.HORIZONTAL)
>    s2.Add(yagut.Label(parent,-1,name =3D "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 ?
>
> All the best !
>
> Norbert
>

I guess you'll get different approaches, and I'd be interested in hearing
about them too, but I find that the way Boa Constructor handles it is useful
for me to keep things straight.  Below is code that is 100% generated by Boa
and took about 10 minutes to do with it.  You can get a sense for how it
segregates sizers, panels, splitters, and notebooks in a way that is pretty
clean, I feel.


#--------------------------------------------------------------------------=
-------------

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1BUTTON3,
 wxID_FRAME1CHECKBOX1, wxID_FRAME1CHECKBOX2, wxID_FRAME1CHECKBOX3,
 wxID_FRAME1CHOICE1, wxID_FRAME1CHOICE2, wxID_FRAME1NOTEBOOK1,
 wxID_FRAME1PANEL1, wxID_FRAME1PANEL10, wxID_FRAME1PANEL11,
 wxID_FRAME1PANEL12, wxID_FRAME1PANEL13, wxID_FRAME1PANEL2,
wxID_FRAME1PANEL3,
 wxID_FRAME1PANEL4, wxID_FRAME1PANEL5, wxID_FRAME1PANEL6, wxID_FRAME1PANEL7,

 wxID_FRAME1PANEL8, wxID_FRAME1PANEL9, wxID_FRAME1RADIOBUTTON1,
 wxID_FRAME1RADIOBUTTON2, wxID_FRAME1RADIOBUTTON3,
wxID_FRAME1SPLITTERWINDOW1,
 wxID_FRAME1STATICTEXT1, wxID_FRAME1STATICTEXT2, wxID_FRAME1TEXTCTRL1,
] =3D [wx.NewId() for _init_ctrls in range(30)]

class Frame1(wx.Frame):
    def _init_coll_boxSizer7_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.flexGridSizer1, 0, border=3D0, flag=3D0)

    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.button1, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.button2, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.button3, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.radioButton1, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.radioButton2, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.radioButton3, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.checkBox1, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.checkBox2, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.checkBox3, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.choice1, 0, border=3D0, flag=3D0)
        parent.AddWindow(self.choice2, 0, border=3D0, flag=3D0)

    def _init_coll_boxSizer6_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.splitterWindow1, 1, border=3D0, flag=3Dwx.EXP=
AND)

    def _init_coll_boxSizer4_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.staticText1, 0, border=3D5, flag=3Dwx.ALL)

    def _init_coll_boxSizer8_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.staticText2, 0, border=3D5, flag=3Dwx.EXPAND)

    def _init_coll_boxSizer5_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.panel9, 0, border=3D0, flag=3Dwx.EXPAND)
        parent.AddWindow(self.textCtrl1, 0, border=3D5, flag=3Dwx.ALL |
wx.EXPAND)
        parent.AddWindow(self.panel10, 1, border=3D0, flag=3Dwx.EXPAND)

    def _init_coll_boxSizer3_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.panel7, 0, border=3D0, flag=3Dwx.GROW | wx.EX=
PAND)
        parent.AddWindow(self.panel8, 1, border=3D0, flag=3Dwx.GROW | wx.EX=
PAND)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.panel2, 0, border=3D0, flag=3Dwx.GROW | wx.EX=
PAND)
        parent.AddWindow(self.panel3, 1, border=3D0, flag=3Dwx.GROW | wx.EX=
PAND)

    def _init_coll_boxSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.notebook1, 1, border=3D10, flag=3Dwx.ALL |
wx.EXPAND)

    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=3D-1, page=3Dself.panel6, select=3DFalse,
              text=3D'Pages0')
        parent.AddPage(imageId=3D-1, page=3Dself.panel5, select=3DTrue,
text=3D'Pages1')
        parent.AddPage(imageId=3D-1, page=3Dself.panel4, select=3DFalse,
              text=3D'Pages2')
        parent.AddPage(imageId=3D-1, page=3Dself.panel13, select=3DFalse,
              text=3D'Pages3')

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer2 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer3 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer4 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer5 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer6 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.boxSizer7 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self.flexGridSizer1 =3D wx.FlexGridSizer(cols=3D3, hgap=3D1, rows=
=3D1,
vgap=3D0)

        self.boxSizer8 =3D wx.BoxSizer(orient=3Dwx.VERTICAL)

        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_boxSizer2_Items(self.boxSizer2)
        self._init_coll_boxSizer3_Items(self.boxSizer3)
        self._init_coll_boxSizer4_Items(self.boxSizer4)
        self._init_coll_boxSizer5_Items(self.boxSizer5)
        self._init_coll_boxSizer6_Items(self.boxSizer6)
        self._init_coll_boxSizer7_Items(self.boxSizer7)
        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)
        self._init_coll_boxSizer8_Items(self.boxSizer8)

        self.panel13.SetSizer(self.boxSizer7)
        self.panel1.SetSizer(self.boxSizer1)
        self.panel2.SetSizer(self.boxSizer8)
        self.panel3.SetSizer(self.boxSizer2)
        self.panel4.SetSizer(self.boxSizer6)
        self.panel5.SetSizer(self.boxSizer5)
        self.panel6.SetSizer(self.boxSizer3)
        self.panel7.SetSizer(self.boxSizer4)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=3DwxID_FRAME1, name=3D'', parent=3Dprnt,
              pos=3Dwx.Point(246, 123), size=3Dwx.Size(402, 489),
              style=3Dwx.DEFAULT_FRAME_STYLE, title=3D'Frame1')
        self.SetClientSize(wx.Size(394, 455))

        self.panel1 =3D wx.Panel(id=3DwxID_FRAME1PANEL1, name=3D'panel1',
parent=3Dself,
              pos=3Dwx.Point(0, 0), size=3Dwx.Size(394, 455),
              style=3Dwx.TAB_TRAVERSAL)

        self.panel2 =3D wx.Panel(id=3DwxID_FRAME1PANEL2, name=3D'panel2',
              parent=3Dself.panel1, pos=3Dwx.Point(0, 0), size=3Dwx.Size(39=
4, 26),
              style=3Dwx.SIMPLE_BORDER | wx.TAB_TRAVERSAL)
        self.panel2.SetBackgroundColour(wx.Colour(255, 255, 255))

        self.panel3 =3D wx.Panel(id=3DwxID_FRAME1PANEL3, name=3D'panel3',
              parent=3Dself.panel1, pos=3Dwx.Point(0, 26), size=3Dwx.Size(3=
94,
429),
              style=3Dwx.TAB_TRAVERSAL)
        self.panel3.SetBackgroundColour(wx.Colour(0, 128, 192))

        self.notebook1 =3D wx.Notebook(id=3DwxID_FRAME1NOTEBOOK1,
name=3D'notebook1',
              parent=3Dself.panel3, pos=3Dwx.Point(10, 10), size=3Dwx.Size(=
374,
409),
              style=3D0)

        self.panel4 =3D wx.Panel(id=3DwxID_FRAME1PANEL4, name=3D'panel4',
              parent=3Dself.notebook1, pos=3Dwx.Point(0, 0), size=3Dwx.Size=
(366,
383),
              style=3Dwx.TAB_TRAVERSAL)

        self.panel5 =3D wx.Panel(id=3DwxID_FRAME1PANEL5, name=3D'panel5',
              parent=3Dself.notebook1, pos=3Dwx.Point(0, 0), size=3Dwx.Size=
(366,
383),
              style=3Dwx.TAB_TRAVERSAL)

        self.panel6 =3D wx.Panel(id=3DwxID_FRAME1PANEL6, name=3D'panel6',
              parent=3Dself.notebook1, pos=3Dwx.Point(0, 0), size=3Dwx.Size=
(366,
383),
              style=3Dwx.TAB_TRAVERSAL)

        self.panel7 =3D wx.Panel(id=3DwxID_FRAME1PANEL7, name=3D'panel7',
              parent=3Dself.panel6, pos=3Dwx.Point(0, 0), size=3Dwx.Size(36=
6, 37),
              style=3Dwx.TAB_TRAVERSAL)
        self.panel7.SetBackgroundColour(wx.Colour(0, 128, 128))

        self.panel8 =3D wx.Panel(id=3DwxID_FRAME1PANEL8, name=3D'panel8',
              parent=3Dself.panel6, pos=3Dwx.Point(0, 37), size=3Dwx.Size(3=
66,
346),
              style=3Dwx.TAB_TRAVERSAL)
        self.panel8.SetBackgroundColour(wx.Colour(255, 255, 0))

        self.staticText1 =3D wx.StaticText(id=3DwxID_FRAME1STATICTEXT1,
              label=3Du'Some text', name=3D'staticText1', parent=3Dself.pan=
el7,
              pos=3Dwx.Point(5, 5), size=3Dwx.Size(115, 27), style=3D0)
        self.staticText1.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))

        self.panel9 =3D wx.Panel(id=3DwxID_FRAME1PANEL9, name=3D'panel9',
              parent=3Dself.panel5, pos=3Dwx.Point(0, 0), size=3Dwx.Size(36=
6,
100),
              style=3Dwx.TAB_TRAVERSAL)
        self.panel9.SetBackgroundColour(wx.Colour(255, 128, 255))

        self.textCtrl1 =3D wx.TextCtrl(id=3DwxID_FRAME1TEXTCTRL1,
name=3D'textCtrl1',
              parent=3Dself.panel5, pos=3Dwx.Point(5, 105), size=3Dwx.Size(=
356,
92),
              style=3D0, value=3D'textCtrl1')

        self.panel10 =3D wx.Panel(id=3DwxID_FRAME1PANEL10, name=3D'panel10',
              parent=3Dself.panel5, pos=3Dwx.Point(0, 202), size=3Dwx.Size(=
366,
181),
              style=3Dwx.TAB_TRAVERSAL)
        self.panel10.SetBackgroundColour(wx.Colour(64, 128, 128))

        self.splitterWindow1 =3D wx.SplitterWindow
(id=3DwxID_FRAME1SPLITTERWINDOW1,
              name=3D'splitterWindow1', parent=3Dself.panel4, pos=3Dwx.Poin=
t(0,
0),
              size=3Dwx.Size(366, 383), style=3Dwx.SP_3D)

        self.panel11 =3D wx.Panel(id=3DwxID_FRAME1PANEL11, name=3D'panel11',
              parent=3Dself.splitterWindow1, pos=3Dwx.Point(0, 0), size=3Dw=
x.Size
(162,
              383), style=3Dwx.TAB_TRAVERSAL)
        self.panel11.SetBackgroundColour(wx.Colour(0, 0, 255))

        self.panel12 =3D wx.Panel(id=3DwxID_FRAME1PANEL12, name=3D'panel12',
              parent=3Dself.splitterWindow1, pos=3Dwx.Point(166, 0),
              size=3Dwx.Size(200, 383), style=3Dwx.TAB_TRAVERSAL)
        self.panel12.SetBackgroundColour(wx.Colour(128, 128, 255))
        self.splitterWindow1.SplitVertically(self.panel11, self.panel12,
162)

        self.panel13 =3D wx.Panel(id=3DwxID_FRAME1PANEL13, name=3D'panel13',
              parent=3Dself.notebook1, pos=3Dwx.Point(0, 0), size=3Dwx.Size=
(366,
383),
              style=3Dwx.TAB_TRAVERSAL)

        self.button1 =3D wx.Button(id=3DwxID_FRAME1BUTTON1, label=3D'button=
1',
              name=3D'button1', parent=3Dself.panel13, pos=3Dwx.Point(0, 0),
              size=3Dwx.Size(75, 23), style=3D0)

        self.button2 =3D wx.Button(id=3DwxID_FRAME1BUTTON2, label=3D'button=
2',
              name=3D'button2', parent=3Dself.panel13, pos=3Dwx.Point(131, =
0),
              size=3Dwx.Size(75, 23), style=3D0)

        self.button3 =3D wx.Button(id=3DwxID_FRAME1BUTTON3, label=3D'button=
3',
              name=3D'button3', parent=3Dself.panel13, pos=3Dwx.Point(262, =
0),
              size=3Dwx.Size(75, 23), style=3D0)

        self.radioButton1 =3D wx.RadioButton(id=3DwxID_FRAME1RADIOBUTTON1,
              label=3D'radioButton1', name=3D'radioButton1', parent=3Dself.=
panel13
,
              pos=3Dwx.Point(0, 23), size=3Dwx.Size(81, 13), style=3D0)

        self.radioButton2 =3D wx.RadioButton(id=3DwxID_FRAME1RADIOBUTTON2,
              label=3D'radioButton2', name=3D'radioButton2', parent=3Dself.=
panel13
,
              pos=3Dwx.Point(131, 23), size=3Dwx.Size(81, 13), style=3D0)

        self.radioButton3 =3D wx.RadioButton(id=3DwxID_FRAME1RADIOBUTTON3,
              label=3D'radioButton3', name=3D'radioButton3', parent=3Dself.=
panel13
,
              pos=3Dwx.Point(262, 23), size=3Dwx.Size(81, 13), style=3D0)

        self.checkBox1 =3D wx.CheckBox(id=3DwxID_FRAME1CHECKBOX1,
label=3D'checkBox1',
              name=3D'checkBox1', parent=3Dself.panel13, pos=3Dwx.Point(0, =
36),
              size=3Dwx.Size(70, 13), style=3D0)

        self.checkBox2 =3D wx.CheckBox(id=3DwxID_FRAME1CHECKBOX2,
label=3D'checkBox2',
              name=3D'checkBox2', parent=3Dself.panel13, pos=3Dwx.Point(131=
, 36),
              size=3Dwx.Size(70, 13), style=3D0)

        self.checkBox3 =3D wx.CheckBox(id=3DwxID_FRAME1CHECKBOX3,
label=3D'checkBox3',
              name=3D'checkBox3', parent=3Dself.panel13, pos=3Dwx.Point(262=
, 36),
              size=3Dwx.Size(70, 13), style=3D0)

        self.choice1 =3D wx.Choice(choices=3D[], id=3DwxID_FRAME1CHOICE1,
              name=3D'choice1', parent=3Dself.panel13, pos=3Dwx.Point(0, 49=
),
              size=3Dwx.Size(130, 21), style=3D0)

        self.choice2 =3D wx.Choice(choices=3D[], id=3DwxID_FRAME1CHOICE2,
              name=3D'choice2', parent=3Dself.panel13, pos=3Dwx.Point(131, =
49),
              size=3Dwx.Size(130, 21), style=3D0)

        self.staticText2 =3D wx.StaticText(id=3DwxID_FRAME1STATICTEXT2,
              label=3Du'Some blue static text', name=3D'staticText2',
              parent=3Dself.panel2, pos=3Dwx.Point(0, 0), size=3Dwx.Size(19=
7, 25),
              style=3D0)
        self.staticText2.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))
        self.staticText2.SetForegroundColour(wx.Colour(0, 0, 255))

        self._init_coll_notebook1_Pages(self.notebook1)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)


if __name__ =3D=3D '__main__':
    app =3D wx.PySimpleApp()
    frame =3D create(None)
    frame.Show()

    app.MainLoop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200803=
15/da37b432/attachment.htm


More information about the wxpython-users mailing list