Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:


import wx
class MyGrid(wx.Frame):
   def __init__(self):
      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])
         grid.Add(current,0,0)
         self.Bind(wx.EVT_BUTTON,self.OnClick,current)
      self.SetSizer(grid)
      self.Fit()
   def OnClick(self,event):
      print event.GetId()
         if event.GetId() == 11:
         self.Destroy()
app = wx.PySimpleApp()
MyGrid().Show()
app.MainLoop()


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:
GridSizer (where rows and columns each have the same width and height)
FlexGridSizer (where each row and column is individually dimensioned)
GridBagSizer (where rows and colums are individually dimensioned, and you can have a widget spanning several rows and / or columns)
BoxSizer (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 as below, or enter http://melksh.am/nnnn for individual articles
Y207 - wxPython
  [903] Pieces of Python - (2006-10-23)
  [2013] wxPython - Introduction and sample - (2009-01-29)
  [2744] PyQt (Python and Qt) and wxPython - GUI comparison - (2010-04-28)
  [2887] Snow Leopard and Python (that is OS X 10.6 and wxPython) - (2010-07-26)
  [3081] wxPython - simple example to add GUI to a server log file analysis - (2010-12-08)
  [3092] wxPython geometry - BoxSizer example - (2010-12-17)
  [3139] Steering our Python courses towards wxPython, SQLite and Django - (2011-01-19)
  [4589] Principles or a GUI and their practical application using wxPthon - (2015-11-30)


Back to
Object Oriented Programming for Structured Programmers - conversion training
Previous and next
or
Horse's mouth home
Forward to
Making the most of critical emails - reading behind the scene
Some other Articles
Matching to a string - what if it matches in many possible ways?
Python regular expressions - repeating, splitting, lookahead and lookbehind
Melksham - two many councils?
Making the most of critical emails - reading behind the scene
Sizers (geometry control) in a wxPython GUI - a first example
Object Oriented Programming for Structured Programmers - conversion training
Can you trust the big brand names?
Python - fresh examples from recent courses
XML handling in Python - SAX, DOM and XSLT examples
4759 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, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3086_Siz ... ample.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb