From: <kc1...@ya...> - 2006-11-01 22:17:12
|
In my quest to try to understand how to use the very confusing WX sizers in= Pythoncard, I came up with this example. I hope that it's useful to other= s.=0A=0A########## Code ################=0A#!/usr/bin/python=0A=0A"""=0A__v= ersion__ =3D "$Revision: 1.5 $"=0A__date__ =3D "$Date: 2004/04/30 16:26:12 = $"=0A"""=0A=0Afrom PythonCard import model=0A=0Aimport wx=0A=0Aclass MyBack= ground(model.Background):=0A=0A def setupSizers(self):=0A=0A comp= =3D self.components=0A=0A #Just think of sizers as component contai= ners that knows how to resize itself (if you set them up correctly)=0A=0A = sizerVert1 =3D wx.BoxSizer(wx.VERTICAL)=0A sizerVert2 =3D wx.B= oxSizer(wx.VERTICAL)=0A sizerVert3 =3D wx.BoxSizer(wx.VERTICAL)=0A = =0A sizerHori1 =3D wx.BoxSizer(wx.HORIZONTAL)=0A sizerH= ori2 =3D wx.StaticBoxSizer(comp.StaticBox1, wx.HORIZONTAL)=0A sizerH= ori3 =3D wx.BoxSizer(wx.HORIZONTAL)=0A=0A btnFlags =3D wx.LEFT | wx.= ALIGN_BOTTOM=0A leftFlags =3D wx.LEFT | wx.TOP | wx.ALIGN_LEFT=0A = rightFlags =3D wx.LEFT | wx.TOP | wx.ALIGN_RIGHT=0A=0A # 2nd ar= gument to the .Add function controls how many "share" of the whole area thi= s entry takes up=0A # A 0 means minimal space=0A # A 1 means = if the other entries are all 0, then it takes up 100%=0A # A "0 1 1"= means the 1st entry takes up minimum space, the other 2 entries shares the= rest of the spaces equally=0A # A "1 1 2" means the 3rd entry takes= up 50% of the space, whereas the other 2 entries shares the remaining 50%= =0A =0A # Place the 2 buttons in a horizontal sizer=0A = sizerHori1.Add(comp.Button2, 0, btnFlags, 5)=0A sizerHori1.Add((5, = 5), 0) # space between the buttons=0A sizerHori1.Add(comp.Button1, = 0, btnFlags, 5)=0A sizerHori1.Add((5, 5), 0) # space between the bu= tton and RHS of the window border=0A=0A # Place all of the buttons i= n a vertical sizer=0A # This sizer is necessary to control vertical = placement of the buttons=0A sizerVert2.Add((5, 5), 0) # space on th= e top side (between the button and the textarea=0A sizerVert2.Add(si= zerHori1, 1, rightFlags)=0A sizerVert2.Add((5, 5), 0) # space on th= e bottom separating it from the bottom of the window=0A=0A # Begin w= orking with the TextArea=0A # This sizer is necessary to control ver= tical placement of the TextArea within the static box=0A sizerVert3.= Add((5, 5), 0) # space between the textarea and the top of the static box= =0A # The 1 is necessary so it will take up all of the spaces within= this sizer when the window is stretched=0A sizerVert3.Add(comp.Text= Area1 , 1, leftFlags | wx.EXPAND)=0A sizerVert3.Add((5, 5), 0) # sp= ace between the textarea and the bottom of the static box=0A=0A # Th= is sizer is necessary to control horizontal placement of the TextArea withi= n the static box=0A sizerHori2.Add((5, 5), 0) # space between the t= extarea and the LHS of the static box=0A # The 1 is necessary so it = will take up all of the spaces within this sizer when the window is stretch= ed=0A sizerHori2.Add(sizerVert3 , 1, leftFlags | wx.EXPAND)=0A = sizerHori2.Add((5, 5), 0) # space between the textarea and the RHS of th= e static box=0A=0A # Place the textarea in sizer sandwitched by spac= ers to the side of the window (10 pixels wide, 5 pixels tall)=0A siz= erHori3.Add((10, 5), 0) # space between the static box and the LHS of the = window=0A sizerHori3.Add(sizerHori2, 1, leftFlags | wx.EXPAND)=0A = sizerHori3.Add((10, 5), 0) # space between the static box and the RHS= of the window=0A=0A # Divide up the main window by two sizers=0A = sizerVert1.Add(sizerHori3, 2, leftFlags | wx.EXPAND)=0A sizerVe= rt1.Add((5, 5), 0) # space between the staticbox and the buttons=0A = sizerVert1.Add(sizerVert2, 0, rightFlags)=0A sizerVert1.Add((5, 5),= 0) # space between the buttons and the bottom of the window=0A sel= f.sizerVert1 =3D sizerVert1=0A=0A sizerVert1.Fit(self)=0A siz= erVert1.SetSizeHints(self)=0A self.panel.SetSizer(sizerVert1)=0A = self.panel.SetAutoLayout(1)=0A self.panel.Layout()=0A =0A= self.Layout() # Not sure if this is necessary but it's suppose to = force an initial resize event=0A =0A #self.visible =3D True= =0A=0A def on_initialize(self, event):=0A # if you have any initi= alization=0A # including sizer setup, do it here=0A self.setu= pSizers()=0A=0A=0Aif __name__ =3D=3D '__main__':=0A app =3D model.Applic= ation(MyBackground)=0A app.MainLoop()=0A=0A=0A######## resource ########= ########=0A{'application':{'type':'Application',=0A 'name':'Templa= te',=0A 'backgrounds': [=0A {'type':'Background',=0A 'name':= 'bgTemplate',=0A 'title':'Standard Template with File->Exit menu',= =0A 'size':(349, 263),=0A 'style':['resizeable'],=0A=0A = 'menubar': {'type':'MenuBar',=0A 'menus': [=0A {'= type':'Menu',=0A 'name':'menuFile',=0A 'label':'&Fi= le',=0A 'items': [=0A {'type':'MenuItem',=0A = 'name':'menuFileExit',=0A 'label':'E&xit= ',=0A 'command':'exit',=0A },=0A = ]=0A },=0A ]=0A },=0A 'components': [= =0A=0A{'type':'StaticBox', =0A 'name':'StaticBox1', =0A 'position':(8= , 2), =0A 'size':(316, 159), =0A 'label':'StaticBox1', =0A },=0A= =0A{'type':'TextArea', =0A 'name':'TextArea1', =0A 'position':(20, 20= ), =0A 'size':(291, 128), =0A 'text':'TextArea1', =0A },=0A=0A{'ty= pe':'Button', =0A 'name':'Button1', =0A 'position':(247, 169), =0A = 'label':'Button1', =0A },=0A=0A{'type':'Button', =0A 'name':'Button2= ', =0A 'position':(167, 169), =0A 'label':'Button2', =0A },=0A=0A]= # end components=0A} # end background=0A] # end backgrounds=0A} }=0A=0A = =0A--=0AJohn Henry=0A=0A |