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.
···
On Sat, Mar 15, 2008 at 11:08 AM, Norbert Klamann Norbert.Klamann@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 = 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 ?
All the best !
Norbert
#---------------------------------------------------------------------------------------
#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,
] = [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=0, flag=0)
def _init_coll_flexGridSizer1_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.button1, 0, border=0, flag=0)
parent.AddWindow(self.button2, 0, border=0, flag=0)
parent.AddWindow(self.button3, 0, border=0, flag=0)
parent.AddWindow(self.radioButton1, 0, border=0, flag=0)
parent.AddWindow(self.radioButton2, 0, border=0, flag=0)
parent.AddWindow(self.radioButton3, 0, border=0, flag=0)
parent.AddWindow(self.checkBox1, 0, border=0, flag=0)
parent.AddWindow(self.checkBox2, 0, border=0, flag=0)
parent.AddWindow(self.checkBox3, 0, border=0, flag=0)
parent.AddWindow(self.choice1, 0, border=0, flag=0)
parent.AddWindow(self.choice2, 0, border=0, flag=0)
def _init_coll_boxSizer6_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.splitterWindow1, 1, border=0, flag=wx.EXPAND)
def _init_coll_boxSizer4_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.staticText1, 0, border=5, flag=wx.ALL)
def _init_coll_boxSizer8_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.staticText2, 0, border=5, flag=wx.EXPAND)
def _init_coll_boxSizer5_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.panel9, 0, border=0, flag=wx.EXPAND)
parent.AddWindow(self.textCtrl1, 0, border=5, flag=wx.ALL | wx.EXPAND)
parent.AddWindow(self.panel10, 1, border=0, flag=wx.EXPAND)
def _init_coll_boxSizer3_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.panel7, 0, border=0, flag=wx.GROW | wx.EXPAND)
parent.AddWindow(self.panel8, 1, border=0, flag=wx.GROW | wx.EXPAND)
def _init_coll_boxSizer1_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.panel2, 0, border=0, flag=wx.GROW | wx.EXPAND)
parent.AddWindow(self.panel3, 1, border=0, flag=wx.GROW | wx.EXPAND)
def _init_coll_boxSizer2_Items(self, parent):
# generated method, don't edit
parent.AddWindow(self.notebook1, 1, border=10, flag=wx.ALL | wx.EXPAND)
def _init_coll_notebook1_Pages(self, parent):
# generated method, don't edit
parent.AddPage(imageId=-1, page=self.panel6, select=False,
text='Pages0')
parent.AddPage(imageId=-1, page=self.panel5, select=True, text='Pages1')
parent.AddPage(imageId=-1, page=self.panel4, select=False,
text='Pages2')
parent.AddPage(imageId=-1, page=self.panel13, select=False,
text='Pages3')
def _init_sizers(self):
# generated method, don't edit
self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer2 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer3 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer4 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer5 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer6 = wx.BoxSizer(orient=wx.VERTICAL)
self.boxSizer7 = wx.BoxSizer(orient=wx.VERTICAL)
self.flexGridSizer1 = wx.FlexGridSizer(cols=3, hgap=1, rows=1, vgap=0)
self.boxSizer8 = wx.BoxSizer(orient=wx.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=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(246, 123), size=wx.Size(402, 489),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(394, 455))
self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
pos=wx.Point(0, 0), size=wx.Size(394, 455),
style=wx.TAB_TRAVERSAL)
self.panel2 = wx.Panel(id=wxID_FRAME1PANEL2, name='panel2',
parent=self.panel1, pos=wx.Point(0, 0), size=wx.Size(394, 26),
style=wx.SIMPLE_BORDER | wx.TAB_TRAVERSAL)
self.panel2.SetBackgroundColour(wx.Colour(255, 255, 255))
self.panel3 = wx.Panel(id=wxID_FRAME1PANEL3, name='panel3',
parent=self.panel1, pos=wx.Point(0, 26), size=wx.Size(394, 429),
style=wx.TAB_TRAVERSAL)
self.panel3.SetBackgroundColour(wx.Colour(0, 128, 192))
self.notebook1 = wx.Notebook(id=wxID_FRAME1NOTEBOOK1, name='notebook1',
parent=self.panel3, pos=wx.Point(10, 10), size=wx.Size(374, 409),
style=0)
self.panel4 = wx.Panel(id=wxID_FRAME1PANEL4, name='panel4',
parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(366, 383),
style=wx.TAB_TRAVERSAL)
self.panel5 = wx.Panel(id=wxID_FRAME1PANEL5, name='panel5',
parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(366, 383),
style=wx.TAB_TRAVERSAL)
self.panel6 = wx.Panel(id=wxID_FRAME1PANEL6, name='panel6',
parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(366, 383),
style=wx.TAB_TRAVERSAL)
self.panel7 = wx.Panel(id=wxID_FRAME1PANEL7, name='panel7',
parent=self.panel6, pos=wx.Point(0, 0), size=wx.Size(366, 37),
style=wx.TAB_TRAVERSAL)
self.panel7.SetBackgroundColour(wx.Colour(0, 128, 128))
self.panel8 = wx.Panel(id=wxID_FRAME1PANEL8, name='panel8',
parent=self.panel6, pos=wx.Point(0, 37), size=wx.Size(366, 346),
style=wx.TAB_TRAVERSAL)
self.panel8.SetBackgroundColour(wx.Colour(255, 255, 0))
self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
label=u'Some text', name='staticText1', parent=self.panel7,
pos=wx.Point(5, 5), size=wx.Size(115, 27), style=0)
self.staticText1.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL,
False, u'Tahoma'))
self.panel9 = wx.Panel(id=wxID_FRAME1PANEL9, name='panel9',
parent=self.panel5, pos=wx.Point(0, 0), size=wx.Size(366, 100),
style=wx.TAB_TRAVERSAL)
self.panel9.SetBackgroundColour(wx.Colour(255, 128, 255))
self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1',
parent=self.panel5, pos=wx.Point(5, 105), size=wx.Size(356, 92),
style=0, value='textCtrl1')
self.panel10 = wx.Panel(id=wxID_FRAME1PANEL10, name='panel10',
parent=self.panel5, pos=wx.Point(0, 202), size=wx.Size(366, 181),
style=wx.TAB_TRAVERSAL)
self.panel10.SetBackgroundColour(wx.Colour(64, 128, 128))
self.splitterWindow1 = wx.SplitterWindow(id=wxID_FRAME1SPLITTERWINDOW1,
name='splitterWindow1', parent=self.panel4, pos=wx.Point(0, 0),
size=wx.Size(366, 383), style=wx.SP_3D)
self.panel11 = wx.Panel(id=wxID_FRAME1PANEL11, name='panel11',
parent=self.splitterWindow1, pos=wx.Point(0, 0), size=wx.Size(162,
383), style=wx.TAB_TRAVERSAL)
self.panel11.SetBackgroundColour(wx.Colour(0, 0, 255))
self.panel12 = wx.Panel(id=wxID_FRAME1PANEL12, name='panel12',
parent=self.splitterWindow1, pos=wx.Point(166, 0),
size=wx.Size(200, 383), style=wx.TAB_TRAVERSAL)
self.panel12.SetBackgroundColour(wx.Colour(128, 128, 255))
self.splitterWindow1.SplitVertically(self.panel11, self.panel12, 162)
self.panel13 = wx.Panel(id=wxID_FRAME1PANEL13, name='panel13',
parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(366, 383),
style=wx.TAB_TRAVERSAL)
self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1',
name='button1', parent=self.panel13, pos=wx.Point(0, 0),
size=wx.Size(75, 23), style=0)
self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label='button2',
name='button2', parent=self.panel13, pos=wx.Point(131, 0),
size=wx.Size(75, 23), style=0)
self.button3 = wx.Button(id=wxID_FRAME1BUTTON3, label='button3',
name='button3', parent=self.panel13, pos=wx.Point(262, 0),
size=wx.Size(75, 23), style=0)
self.radioButton1 = wx.RadioButton(id=wxID_FRAME1RADIOBUTTON1,
label='radioButton1', name='radioButton1', parent=self.panel13,
pos=wx.Point(0, 23), size=wx.Size(81, 13), style=0)
self.radioButton2 = wx.RadioButton(id=wxID_FRAME1RADIOBUTTON2,
label='radioButton2', name='radioButton2', parent=self.panel13,
pos=wx.Point(131, 23), size=wx.Size(81, 13), style=0)
self.radioButton3 = wx.RadioButton(id=wxID_FRAME1RADIOBUTTON3,
label='radioButton3', name='radioButton3', parent=self.panel13,
pos=wx.Point(262, 23), size=wx.Size(81, 13), style=0)
self.checkBox1 = wx.CheckBox(id=wxID_FRAME1CHECKBOX1, label='checkBox1',
name='checkBox1', parent=self.panel13, pos=wx.Point(0, 36),
size=wx.Size(70, 13), style=0)
self.checkBox2 = wx.CheckBox(id=wxID_FRAME1CHECKBOX2, label='checkBox2',
name='checkBox2', parent=self.panel13, pos=wx.Point(131, 36),
size=wx.Size(70, 13), style=0)
self.checkBox3 = wx.CheckBox(id=wxID_FRAME1CHECKBOX3, label='checkBox3',
name='checkBox3', parent=self.panel13, pos=wx.Point(262, 36),
size=wx.Size(70, 13), style=0)
self.choice1 = wx.Choice(choices=[], id=wxID_FRAME1CHOICE1,
name='choice1', parent=self.panel13, pos=wx.Point(0, 49),
size=wx.Size(130, 21), style=0)
self.choice2 = wx.Choice(choices=[], id=wxID_FRAME1CHOICE2,
name='choice2', parent=self.panel13, pos=wx.Point(131, 49),
size=wx.Size(130, 21), style=0)
self.staticText2 = wx.StaticText(id=wxID_FRAME1STATICTEXT2,
label=u'Some blue static text', name='staticText2',
parent=self.panel2, pos=wx.Point(0, 0), size=wx.Size(197, 25),
style=0)
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 == ‘main’:
app = wx.PySimpleApp()
frame = create(None)
frame.Show()
app.MainLoop()