[Tutor] How To structure a program for cmd line mode & gui mode

Alan Gauld alan.gauld at btinternet.com
Fri Dec 1 09:47:41 CET 2006

"Tony Cappellini" <cappy2112 at gmail.com> wrote

> I would like to find out how to structure the program so that when 
> the gui
> version is finised, it will still be fully functional in cmd line 
> mode

For complex GUIs this can be quite difficult since GUIs are
essentially stateless and command lines are usually stateful.

> The gui itself will be very simple- A listbox, and button or two.

For simple GUIs you are usually just collecting data and
then executing one of several *transactions*. To do this via
a command line is fairly simple. Just present a text menu
of the possible transactions, then capture the data needed
for the chosen transavction, then fire the transaction.

The key thing is that the transaction code should be UI neutral,
it should not have any form of input other than its parameters
and it should retuirn its result as a string, or a tuple (or 
of values that the UI can display.

Combining GUI and Command versions in the same program
just means reading the options at startup and calling the
appropriate start UI function.

Use modules to separate your concerns - one main, one for the GUI,
one for the command line UI and one for the shared transactions.
Import the shared module into both GUI modules and import
both GUI modules into your main module.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list