Sizers (geometry control) in a wxPython GUI - a first example
In all GUI (Graphic User Interfaces), you define components / widgets, you define how they're laid out, and you define what's to happen when an even occurs, such as a button is pressed. All this definition can take quite a lot oflines of code, and it's not uncommon to find even 'simple' examples in the textbooks that are over a page long.
So today, I set out to write a really simple GUI in wxPython and make it as short as possible while showing
• Creation of components
• AUTOMATED component layout
• Handling of events on those components.
Here it is - 19 lines / 586 bytes:
wx.Frame.__init__(self,None,-1,"First Sizer Demo")
grid = wx.GridSizer(rows=2, cols=2, hgap=20, vgap=30)
cellname = ("Angie","Babs","Cindy","Debbie-Alexandra")
for cell in range(len(cellname)):
current = wx.Button(self,cell+10,cellname[cell])
if event.GetId() == 11:
app = wx.PySimpleApp()
COMMENTED source available [here]
My decision to automate the layout of the components is a significant one; I had the option of defining each of them by pixel position, but although that's more straightforward for your very first wxPython GUI, it also leads to longer and far less flexible code, and I recommend that you should use a Sizer
in all but the most exceptional of circumstances. Of course, the example above just starts to touch on Sizers - you can choose from:
(where rows and columns each have the same width and height)
(where each row and column is individually dimensioned)
(where rows and colums are individually dimensioned, and you can have a widget spanning several rows and / or columns)
(where you have a single row or single column of widgets)
and you can nest sizers within sizers to give far greater flexibility - "almost any layout you like"
Going further, widgets won't always fill cells in your sizer, and you have a range of control as to the gravity of the widgets - whether they rise to the top of the call, fall to the bottom, or float in the middle. And you can also control whether the widgets expand to fill the cell, or are surrounded by background. If you look at the illustration at the top of this article, you'll see that the default is that the widgets gravitate to the top left of the cell.
Python has several GUIs available - but wxPython is perhaps the most popular, and we include a brief introduction in our Python Programming
and Learning to program in Python
courses. If you're going to be making heavy use of the wx GUI, and you're new to GUI programming, please ask us to extent the course by a day (which we can do for even one delegate) to introduce you to the wonderful art of designing and writing GUIs! (written 2010-12-15)
Associated topics are indexed underY207 - wxPython 
Steering our Python courses towards wxPython, SQLite and Django - (2011-01-19) 
wxPython geometry - BoxSizer example - (2010-12-17) 
wxPython - simple example to add GUI to a server log file analysis - (2010-12-08) 
Snow Leopard and Python (that is OS X 10.6 and wxPython) - (2010-07-26) 
PyQt (Python and Qt) and wxPython - GUI comparison - (2010-04-28) 
wxPython - Introduction and sample - (2009-01-29) 
Pieces of Python - (2006-10-23)
Some other Articles
Matching to a string - what if it matches in many possible ways?Python regular expressions - repeating, splitting, lookahead and lookbehindMelksham - two many councils?Making the most of critical emails - reading behind the sceneSizers (geometry control) in a wxPython GUI - a first exampleObject Oriented Programming for Structured Programmers - conversion trainingCan you trust the big brand names? Python - fresh examples from recent coursesXML handling in Python - SAX, DOM and XSLT examples
4090 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).