[Tutor] Adding a GUI
Alan Gauld
alan.gauld at btinternet.com
Sun Sep 16 10:02:19 CEST 2007
"wormwood_3" <wormwood_3 at yahoo.com> wrote
> I am just starting to learn GUI programming, with wxPython.
Good choice. If you will be doing much in wxPython get the book.
It makes the whole thing much easier.
> The script heretofore was just run at the command line.
> Would it make sense to add an option in the same script
> to have it run in "graphical mode",
You can do but personally I prefer to have two separate
versions, one with a CLI and the other a GUI. They both
share a common module which is the core functionality
represented as functions/classes so that the UI (either of them)
can call them as needed.
To do that the first step is to identify the core functions and
refactor them into a separate module (or modules). To do
that you have to remove all UI operations - print/raw_input etc
from the functions and make them completely parameterised.
(ideally they should be stateless too) The strings and data
you need to display will be returned by the functions.
Once you have that done its easy to add a CLI a GUI and
even a web interface.
> Anyone have any tips on adding a GUI to a pre-existing
> script in a way that minimizes repetition of logic?
I go through the process in the case study in my tutorial.
Just scan through to the section Adding a GUI. It uses
Tkinter but the conepts are identical.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list