wxPython - BoxSizer



This sizer allows the controls to be arranged in row-wise or column-wise manner. BoxSizer’s layout is determined by its orientation argument (either wxVERTICAL or wxHORIZONTAL).

Box = wx.BoxSizer(wxHORIZONTAL)
Box = wx.BoxSizer(wxVERTICAL)

Add() method (inherited from wxSizer) appends it to the next row/column of the sizer.

Box.Add(control, proportion, flag, border)

The proportion parameter controls how the control changes it size in response to dimensions of the container. Combination of various flag parameters decides the appearance of control in the sizer. Following are some of the flags −

Alignment Flags

wx.ALIGN_TOP
wx.ALIGN_BOTTOM
wx.ALIGN_LEFT
wx.ALIGN_RIGHT
wx.ALIGN_CENTER_VERTICAL
wx.ALIGN_CENTER_HORIZONTAL

Border Flags

wx.TOP
wx.BOTTOM
wx.LEFT
wx.RIGHT
wx.ALL

Behavior Flags

S.N. Behavior Flags & Description
1

wx.EXPAND

Item will expand to fill the space provided to it (wx.GROW is the same)

2

wx.SHAPED

Similar to EXPAND but maintains the item's aspect ratio

3

wx.FIXED_MINSIZE

Does not let the item become smaller than its initial minimum size

4

wx.RESERVE_SPACE_EVEN_IF_ HIDDEN

Does not allow the sizer to reclaim an item's space when it is hidden

The border parameter is an integer, the space in pixels to be left between controls. For example,

b = wx.StaticText(self, -1, “Enter a number”) 
Box.Add(b,1,wx.ALL|wx.EXPAND,10) 

Following are some more methods of wx.BoxSizer class −

S.N. Methods & Description
1

SetOrientation()

Sets orientation wxHORIZONTAL or wxVERTICAL

2

AddSpacer()

Adds nonstretchable space

3

AddStretchSpacer()

Adds stretchable space so that resizing the window will affect control size proportionately

4

Clear()

Removes children from sizer

5

Detach()

Removes a control from sizer without destroying

6

Insert()

Inserts a child control at a specified position

7

Remove()

Removes a child from sizer and destroys it

Example

In the following code, a vertical box sizer is applied to a panel object which is placed inside wxFrame window.

p = wx.Panel(self) 
vbox = wx.wx.BoxSizer(wx.VERTICAL)

The first row in the box displays a label (wx.StaticText object) in the center with a border of 20 pixels around it.

l1 = wx.StaticText(p,label = "Enter a number",style = wx.ALIGN_CENTRE ) 
vbox.Add(l1,0,  wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 20)

In the second row, a wx.Button object is displayed. Because of wx.EXPAND flag it occupies the entire width of the window.

b1 = wx.Button(p, label = "Btn1") 
vbox.Add(b1,0, wx.EXPAND)

The next row also contains a button. It is not added with EXPAND flag. Instead, because of ALIGN_CENTER_HORIZONTAL, button with default size appears in the center horizontally.

b2 = wx.Button(p, label = "Btn2") 
vbox.Add(b2,0,wx.ALIGN_CENTER_HORIZONTAL)

In the next row, a TextCtrl object with proportion parameter set to 1 and EXPAND flag set is added. As a result, it is taller in size.

t = wx.TextCtrl(p) 
vbox.Add(t,1,wx.EXPAND,10)

The last row holds a horizontal sizer object, which in turn has a label and button separated by a stretchable space.

hbox = wx.BoxSizer(wx.HORIZONTAL) 
l2 = wx.StaticText(p,label = "Label2", style = wx.ALIGN_CENTRE) 
hbox.Add(l2,0,wx.EXPAND) 

b3 = wx.Button(p,label = "Btn3") 
hbox.AddStretchSpacer(1) 
hbox.Add(b3,0,wx.ALIGN_LEFT,20) 
vbox.Add(hbox,1,wx.ALL|wx.EXPAND)

Lastly, the vertical box sizer is applied to wx.Panel object.

Following is the complete code −

import wx 
 
class Example(wx.Frame): 
   
   def __init__(self, parent, title): 
      super(Example, self).__init__(parent, title = title, size = (200,300)) 
             
      self.InitUI() 
      self.Centre() 
      self.Show()
		
   def InitUI(self): 
      p = wx.Panel(self) 
      vbox = wx.wx.BoxSizer(wx.VERTICAL) 
      l1 = wx.StaticText(p,label = "Enter a number",style = wx.ALIGN_CENTRE ) 
      vbox.Add(l1,0,  wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 20) 
      b1 = wx.Button(p, label = "Btn1") 
      vbox.Add(b1,0,wx.EXPAND) 
          
      b2 = wx.Button(p, label = "Btn2") 
      vbox.Add(b2,0,wx.ALIGN_CENTER_HORIZONTAL) 
      t = wx.TextCtrl(p) 
      vbox.Add(t,1,wx.EXPAND,10) 
      hbox = wx.BoxSizer(wx.HORIZONTAL) 
      l2 = wx.StaticText(p,label = "Label2", style = wx.ALIGN_CENTRE) 
		
      hbox.Add(l2,0,wx.EXPAND) 
      b3 = wx.Button(p,label = "Btn3") 
      hbox.AddStretchSpacer(1) 
      hbox.Add(b3,0,wx.ALIGN_LEFT,20) 
      vbox.Add(hbox,1,wx.ALL|wx.EXPAND) 
      p.SetSizer(vbox) 
          
app = wx.App() 
Example(None, title = 'BoxSizer demo') 
app.MainLoop()

The above code produces the following output −

BoxSizer Output
wxpython_layout_management.htm
Advertisements