Principles or a GUI and their practical application using wxPthon
Archive - Originally posted on "The Horse's Mouth" - 2015-11-30 22:02:52 - Graham EllisFrom today's training - a private course that was brief Python revision followed by an introduction to GUIs (in the form of wxPython ...
I started with a generic intordcution to GUIs ... written in the form of a source code file in Python:
# principles of a GUI
#
# Provides a "Framework" and "Helpers"
#
# (somewhere here - load / initialse model)
# 1. Define components ("widgets")
# 2. Define a geometry ("layout")
# 3. Define my events
#
# BEFORE you
#
# (4. Display initial view)
# 5. Await an event
# 6. Process that event
# 7. go to step 5
and then I went on to add real code into each of the sections. Here is the implementation for each step:
# Provides a "Framework" and "Helpers"
import wx
# load / initialse model
import mymodel
# Create an empty shell for the various els.
app = wx.PySimpleApp()
class RailStationApp(wx.Frame):
def __init__(this):
wx.Frame.__init__(this, None,wx.ID_ANY,"About rail places", (600,200))
# 1. Define components ("widgets")
this.myWoof = wx.Button(this,1,"Gypsy")
this.myYap = wx.Button(this,2,"Billy")
# 2. Define a geometry ("layout")
this.geom = wx.BoxSizer(wx.VERTICAL)
this.geom.Add(this.myWoof)
this.geom.Add(this.myYap)
this.SetSizer(this.geom)
this.SetAutoLayout(1)
this.geom.Fit(this)
# 3. Define my events
this.Bind(wx.EVT_BUTTON,this.feed,this.myWoof)
this.Bind(wx.EVT_BUTTON,this.walk,this.myYap)
def feed(this,event): print "We are out of Bonios"
def walk(this,event): print "Peeing down with rain"
# 4. Display initial view
this.Show(True)
# 5. Await an event
# 6. Process that event
# 7. go to step 5
app.MainLoop()

Having put the rudiments of each step into place, I moved on to ...
Add a label and change event handlers so that feedback appeared within the GUI (see [here].
Add a whole list of data elements and a loop of buttons, with a single event handler working for all of the buttons [here]
And finally switching to use data from my model to load the GUI and present dynamic information [here]
Image - samples of the four application examples