[Tutor] Developing a GUI application

Luke Paireepinart rabidpoobear at gmail.com
Fri Mar 23 22:50:41 CET 2007


Terry Carroll wrote:
> I'm pretty much just a dabbler in Python.
>
> Up to now I've mostly been doing line-oriented programs, but I have a 
> small app I want to write that I think would be a good candidate to write 
> as a GUI-based app.
>
> I'd like advice from the more seasoned programmers here: how do you 
> approach a GUI-based app?  
>
> I figure one way to do it is to write the nuts and bolts of it using plain
> old line oriented techniques; and then write the GUI, calling the
> functions already written.
>
> The other way would be to write a GUI shell with all the controls, but 
> calling dummy functions; then write the functions to do the tasks.
>   
In either case, what you're intending to end up with is a decoupling of 
the GUI code and the program logic.

For example, in a command-line program, something like this:
print  "%i is your number!" % int(raw_input( "Please enter a number to 
be squared! " )) ** 2

Has the display code and the program logic all tangled up.
A much better version would be:
def squareNum(anint):  return anint ** 2

num = int(raw_input("enter a number to be squared, please! "))
print "Your number squared is: " + str(squareNum(num))

Because if later you want to reuse your function in a GUI app, you'd 
just change your value inputs but the functions would remain the same.

So essentially, when you write your GUI app, try to keep all 
calculations in functions with no GUI involvement.  It makes your 
functions more reusable and also it's good practice, as well as easier 
to maintain ( not having to look through GUI code to find where your 
calculations are.)

I'd say that it'd be best to make it a command-line app first, so that 
you can test the functionality of your code.  Essentially, the GUI is 
just an interface to the functions of your program that lets people use 
your program  easier, and there's no reason to write that part first 
(IMHO of course).
It would be a good idea to sketch out your GUI on a sheet of paper, so 
you have ideas of what kind of paramaters your functions will need to 
use before you write them.  For example, you might want function 
DisplayCDInfo to take an artist and an album, and write the function for 
that, then later realize that you wanted your GUI to give them the 
ability to show the track number as well.  It's nice to know (generally) 
what requirements you want before you start writing a program.
> (If it matters: I'm going to use wxPython; and my app is yet another CDROM 
> database program; more details available if it matters.)
Sounds pretty interesting, mind elaborating a bit?  Is it just for 
retrieving the CD info from freecddb and whatnot and display it?


More information about the Tutor mailing list