In this tutorial, we will create a form based on the example below using only wx.BoxSizers.

Example: Simple data collection form where icon and text are right justified along the vertical axis of colons.

icon - title
separator
icon - text: - single line input control
icon - text: - single line input control
icon - text: - single line input control
icon - text: - multi-line text/list control that stretches vertically
separator
ok - cancel

First, we’ll create a wx.Frame to contain all the widgets and we’ll create a wx.Panel to “skin” the frame so that it looks normal on all platforms.

   1 import wx
   2 
   3 class MyForm(wx.Frame):
   4 
   5     def __init__(self):
   6         wx.Frame.__init__(self, None, wx.ID_ANY, title='My Form')
   7 
   8         # Add a panel so it looks correct on all platforms
   9         self.panel = wx.Panel(self, wx.ID_ANY)
  10 
  11 if __name__ == '__main__':
  12     app = wx.PySimpleApp()
  13     frame = MyForm().Show()
  14     app.MainLoop()

The next step is to figure out how to create an icon. I will be using wx.ArtProvider for the bitmap since it offers generic cross-platform bitmaps/icons and I’ll place the bitmap in a wx.StaticBitmap widget. Check out the sample code below:

   1 bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_OTHER, (16, 16))
   2 titleIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)

Let’s go over what’s happening here. The first argument to GetBitmap is the art id, the second is the client (such as wx.ART_TOOLBAR or wx.ART_MENU) and the third is the size of the icon. The StaticBitmap’s arguments are pretty much self-explanatory. You can see both in action in the wxPython Demo as well.

Next we’ll put all the code together and stick them in sizers. I’ll be using BoxSizer’s only for this example.

   1 import wx
   2 
   3 class MyForm(wx.Frame):
   4 
   5     def __init__(self):
   6         wx.Frame.__init__(self, None, wx.ID_ANY, title='My Form')
   7 
   8         # Add a panel so it looks correct on all platforms
   9         self.panel = wx.Panel(self, wx.ID_ANY)
  10 
  11         bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_OTHER, (16, 16))
  12         titleIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
  13         title = wx.StaticText(self.panel, wx.ID_ANY, 'My Title')
  14 
  15         bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_OTHER, (16, 16))
  16         inputOneIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
  17         labelOne = wx.StaticText(self.panel, wx.ID_ANY, 'Input 1')
  18         inputTxtOne = wx.TextCtrl(self.panel, wx.ID_ANY, '')
  19 
  20         inputTwoIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
  21         labelTwo = wx.StaticText(self.panel, wx.ID_ANY, 'Input 2')
  22         inputTxtTwo = wx.TextCtrl(self.panel, wx.ID_ANY,'')
  23 
  24         inputThreeIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
  25         labelThree = wx.StaticText(self.panel, wx.ID_ANY, 'Input 3')
  26         inputTxtThree = wx.TextCtrl(self.panel, wx.ID_ANY, '')
  27 
  28         inputFourIco = wx.StaticBitmap(self.panel, wx.ID_ANY, bmp)
  29         labelFour = wx.StaticText(self.panel, wx.ID_ANY, 'Input 4')
  30         inputTxtFour = wx.TextCtrl(self.panel, wx.ID_ANY, '')
  31 
  32         okBtn = wx.Button(self.panel, wx.ID_ANY, 'OK')
  33         cancelBtn = wx.Button(self.panel, wx.ID_ANY, 'Cancel')
  34         self.Bind(wx.EVT_BUTTON, self.onOK, okBtn)
  35         self.Bind(wx.EVT_BUTTON, self.onCancel, cancelBtn)
  36 
  37         topSizer        = wx.BoxSizer(wx.VERTICAL)
  38         titleSizer      = wx.BoxSizer(wx.HORIZONTAL)
  39         inputOneSizer   = wx.BoxSizer(wx.HORIZONTAL)
  40         inputTwoSizer   = wx.BoxSizer(wx.HORIZONTAL)
  41         inputThreeSizer = wx.BoxSizer(wx.HORIZONTAL)
  42         inputFourSizer  = wx.BoxSizer(wx.HORIZONTAL)
  43         btnSizer        = wx.BoxSizer(wx.HORIZONTAL)
  44 
  45         titleSizer.Add(titleIco, 0, wx.ALL, 5)
  46         titleSizer.Add(title, 0, wx.ALL, 5)
  47 
  48         inputOneSizer.Add(inputOneIco, 0, wx.ALL, 5)
  49         inputOneSizer.Add(labelOne, 0, wx.ALL, 5)
  50 
  51         inputOneSizer.Add(inputTxtOne, 1, wx.ALL|wx.EXPAND, 5)
  52 
  53         inputTwoSizer.Add(inputTwoIco, 0, wx.ALL, 5)
  54         inputTwoSizer.Add(labelTwo, 0, wx.ALL, 5)
  55         inputTwoSizer.Add(inputTxtTwo, 1, wx.ALL|wx.EXPAND, 5)
  56 
  57         inputThreeSizer.Add(inputThreeIco, 0, wx.ALL, 5)
  58         inputThreeSizer.Add(labelThree, 0, wx.ALL, 5)
  59         inputThreeSizer.Add(inputTxtThree, 1, wx.ALL|wx.EXPAND, 5)
  60 
  61         inputFourSizer.Add(inputFourIco, 0, wx.ALL, 5)
  62         inputFourSizer.Add(labelFour, 0, wx.ALL, 5)
  63         inputFourSizer.Add(inputTxtFour, 1, wx.ALL|wx.EXPAND, 5)
  64 
  65         btnSizer.Add(okBtn, 0, wx.ALL, 5)
  66         btnSizer.Add(cancelBtn, 0, wx.ALL, 5)
  67 
  68         topSizer.Add(titleSizer, 0, wx.CENTER)
  69         topSizer.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND, 5)
  70         topSizer.Add(inputOneSizer, 0, wx.ALL|wx.EXPAND, 5)
  71         topSizer.Add(inputTwoSizer, 0, wx.ALL|wx.EXPAND, 5)
  72         topSizer.Add(inputThreeSizer, 0, wx.ALL|wx.EXPAND, 5)
  73         topSizer.Add(inputFourSizer, 0, wx.ALL|wx.EXPAND, 5)
  74         topSizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 5)
  75         topSizer.Add(btnSizer, 0, wx.ALL|wx.CENTER, 5)
  76 
  77         self.panel.SetSizer(topSizer)
  78         topSizer.Fit(self)
  79 
  80 
  81     def onOK(self, event):
  82         # Do something
  83         print 'onOK handler'
  84 
  85     def onCancel(self, event):
  86         self.closeProgram()
  87 
  88     def closeProgram(self):
  89         self.Close()
  90 
  91 
  92 # Run the program
  93 if __name__ == '__main__':
  94     app = wx.PySimpleApp()
  95     frame = MyForm().Show()
  96     app.MainLoop()

If you run this code, you should see something like this:

Screenshot-My Form.png

So, how do these sizers work anyway? Let’s take a look. Here’s the basic style:

   1 mySizer.Add(window, proportion, flag(s), border, userData)

The first argument can be a window/widget, sizer or size. The second is a proportion, which allows the developer to specify how much an item is stretched when the parent window is resized. The third is a flag or series of flags which control alignment, border and resizing. The fourth argument is the border, which is the number of pixels of white space around the widget that’s been added. The final one is userData, which I never use. However, according the “wxPython in Action” book, it’s used for passing in extra data to the sizer for its algorithm.

The three bit flags I use are wx.ALL, wx.EXPAND and wx.CENTER: wx.ALL is used to put x number of pixels on all sides of the widget; wx.EXPAND tells the sizer to allow the widget to expand or stretch when the parent is stretched; wx.CENTER will center the widget horizontally and vertically within the widget.

As you can see, I created a separate BoxSizer for any set of two or more widgets that needed to be aligned next to each other horizontally. I also created one master BoxSizer that was oriented vertically so that I could “stack” the other sizers in it. I also stuck in two wx.StaticLine widgets as separators at the appropriate places.

Finally, I connect the panel to the topSizer using the SetSizer() method. I also decided to use the sizer’s Fit() method to tell the sizer to calculate the size based on the window (i.e. the frame). You could also set it up by setting a minimum size for the containing widget using the SetMinSize() method.

Now you have learned the basics of setting up a form using BoxSizers. att Further Reading: attachment::boxsize.py

BoxSizerTutorial (last edited 2010-03-10 17:30:52 by c-24-21-95-71)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.