This is the tutorial used for OSCON. This tutorial centers around revisions 18 through 35 of paster-demo.py. The revisions have been ordered to deliberately start from a very simple working example (revision 18) and build it incrementally into something more capable, with almost every intermediate version being a respectable starting point for a new project.
Stage 1: Very basic version
The absolute simplest version of the program:
- rev 18: paster-demo.py
This version doesn't do anything other than provide field editing. We have to hit ctrl-c to exit out of this version.
Stage 2: Making it minimally useful
The goal of this step is to get the basics of the form implemented.
- rev 19: paster-demo.py (diff: 6c1e85c9e3cf ) add ok and cancel buttons
- rev 20: paster-demo.py (diff: aeaa33461c8b ) wire up ok/cancel buttons to raise an exception
- rev 21: paster-demo.py (diff: eac355fe9f5c ) print out the field values
- rev 22: paster-demo.py (diff: ec57d868ba91 ) Changed zipsafe field into a checkbox
At this point, it's not very pretty, but it does most of what you'd want the software to do. All of the fields are rendered, and the values are captured in a way that they can be retrieved.
Stage 3: Some initial refactoring
Up until now, the program was one big "main" function.
- rev 23: paster-demo.py (diff: 150f1f44e820 ) Split out get_buttons into a function
- rev 24: paster-demo.py (diff: 7ffda2d3a235 ) Split get_field into a function
At the end of this stage, a couple functions have been split out.
Stage 4: abstracting away field value management
Attempting to make some simple visual changes makes it obvious that a little more refactoring will probably be needed.
- rev 25: paster-demo.py (diff: 20cc80968422 ) moved labels and fields into separate columns
- rev 26: paster-demo.py (diff: 96d9164975c7 ) created FieldManager class to abstract the field value management from the widgets
Creating the FieldManager class liberates us from worrying about how to retrieve the field values in the end. Without it, we mess up our ability to get the field values every time we move the widget around or nest it inside of some other widget.
Stage 5: Adding a frame, and providing ESC to exit
Now we can add a frame to give ourselves a persistent title at the top of the window, even when the top of our form scrolls out of view. We take advantage of the opportunity to do one last bit of refactoring here, then add the ability to exit using the ESC key.
- rev 27: paster-demo.py (diff: 936542803102 ) added a header
- rev 28: paster-demo.py (diff: f328230955e6 ) broke out get_header() and get_body()
- rev 29: paster-demo.py (diff: 748a93e134bd ) Allow ESC key to exit the software
Stage 6: Spacing
- rev 30: paster-demo.py (diff: ab5e60f89597 ) Added Divider elements for better vertical spacing
- rev 31: paster-demo.py (diff: 572641d33af6 ) added left and right margin/padding
- rev 32: paster-demo.py (diff: 7046c085745d ) Used GridFlow to add horizontal padding around buttons and make them smaller
- rev 33: paster-demo.py (diff: ac7c0f438bf5 ) Added weights to columns to make the fields bigger in relation to the labels
- rev 34: paster-demo.py (diff: 1a375cd23dd7 ) Put hanging asterisk off to the left, and lined up colons next to the fields
Stage 7: Color