[Tutor] Best Code testing practice?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 20 18:01:34 CEST 2013


On 20/06/13 16:11, Matt D wrote:

> right make a small sample app.  exactly.  im sorry if im dense or
> whatever and obviously i am super new to this process. but i can write
> the scripts in gedit and then what?  how do i run that file and make the
> gui start? should i be doing something like this?:
>
>>>> execfile('filename.py')

Probably not. You could just run the GUI directly:

python guifile.py

Then just play with it. If you define the eventhandlers
to print messages you can see which method is getting
called by each widget etc. But you won;t be using
the >>> prompt.

If you really want to drive it from the interpreter - maybe
to examine status fields etc - then the best thing is to make the
GUI a class and then import your file and create an instance of
it. You can then send messages to the object to exercise its
behaviour.

# file testGUI.py
class MyGUI
     def __init__(self):
         self.buildGUI()

     def eventHandler_1(self):
        # do something in response to event1
        # maybe print a message or log the GUI data in a file
     def eventHandler_2(self):
        # same for event 2 etc...

     def buildGUI(self):
         # build your GUI here including your tab.
         # bind any widgets to use the event handlers defined above


Now, in the interpreter:

 >>> import testGUI
 >>> app = testGUI.MyGUI()  # GUI should appear for you to play with
 >>> app.eventHandler_1()   # test the event handlers independent of GUI
 >>> print app.myVariable   # check a GUI variable value

Does that help?


PS.
Not sure how this will come through. Thunderbird has changed font colour 
half way through and I can't figure out how to change it back - which 
makes me suspicious!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list