Next / Previous / Contents / TCC Help System / NM Tech homepage

4. scrolledlisttest: A small test driver

Here is a small, complete Tkinter application that exercises the functions of the ScrolledList widget. This is what it looks like:

We start out with the usual prologue: a comment pointing back at this documentation. We import the Tkinter module, then we import the module under test: ScrolledList.

scrolledlisttest
#!/usr/bin/env python
#================================================================
# scrolledlisttest: Test driver for ScrolledList
#
# For documentation, see:
#   http://www.nmt.edu/tcc/help/lang/python/examples/scrolledlist/
#----------------------------------------------------------------

from Tkinter import *
import scrolledlist

4.1. The Application class

Next we start declaring the Application class, which embodies the entire graphical user interface.

scrolledlisttest
class Application(Frame):
    """GUI for scrolledlisttest
    """

4.2. Application.__init__(): Constructor

The constructor is quite pro forma: call the parent class constructor, register the application with its .grid() method, create the widgets, and run some initial tests.

scrolledlisttest
    def __init__ ( self ):
        """Constructor for the Application class.
        """
        Frame.__init__ ( self, None )
        self.grid()
        self.__createWidgets()
        self.__tests()

4.3. Application.__createWidgets(): Widget layout

There are only two widgets: the ScrolledList widget we are testing, and a quit button below it.

scrolledlisttest
    def __createWidgets ( self ):
        """Lay out the widgets.
        """
        self.sbox = scrolledlist.ScrolledList ( self,
            width=20, height=10, hscroll=1,
            callback=self.__pickHandler )
        self.sbox.grid ( row=0, column=0 )

        self.quitButton  =  Button ( self, text="Quit",
            command=self.quit )
        self.quitButton.grid ( row=1, column=0, columnspan=99,
            sticky=E+W, ipadx=5, ipady=5 )

4.4. Application.__pickHandler(): Handler for clicking on the listbox

This method is the callback function that gets called whenever the user clicks on a line in the listbox. It displays the line number and the text on that line.

scrolledlisttest
    def __pickHandler ( self, linex ):
        """Handler for user clicks on lines in the listbox.
        """
        print "Click on line %d, '%s'" % (linex, self.sbox[linex])

4.5. Application.__tests__(): Run initial tests

This method adds some lines to the listbox using both the .append() and .insert() methods, exercises the .delete() method, and then tests the .count() method.

scrolledlisttest
    def __tests ( self ):
        """Initial testing of the ScrolledList widget.
        """

First we insure that the initial list length is zero. Then we add three lines and again check the length.

scrolledlisttest
        print "Initial size is", self.sbox.count()
        print "Add alpaca, buffalo, eagle:"
        self.sbox.append ( "alpaca" )
        self.sbox.append ( "buffalo" )
        self.sbox.append ( "eagle" )
        print "Size is now", self.sbox.count()

Next we test the .clear() method. The size after clearing should be zero.

scrolledlisttest
        print "Clear listbox:"
        self.sbox.clear()
        print "Size is now", self.sbox.count()

We add the same three lines back in, then test insertion.

scrolledlisttest
        print "Add alpaca, buffalo, eagle:"
        self.sbox.append ( "alpaca" )
        self.sbox.append ( "buffalo" )
        self.sbox.append ( "eagle" )
        print "Insert cachalot"
        self.sbox.insert ( 2, "cachalot" )
        print "Size is now", self.sbox.count()

Next we test deletion.

scrolledlisttest
        print "Delete buffalo:"
        self.sbox.delete ( 1 )
        print "Size is now", self.sbox.count()

Finally, we insert enough lines, and one long line, to make it possible to test the scrollbars.

scrolledlisttest
        print "Insert bunches o stuff"
        self.sbox.append ( "finch" )
        self.sbox.append ( "goshawk" )
        self.sbox.append ( "harrier" )
        self.sbox.append ( "indigobird" )
        self.sbox.append ( "jabiru" )
        self.sbox.append ( "kingfisher" )
        self.sbox.append ( "Middendorff's grasshopper-warbler" )
        self.sbox.append ( "merlin" )
        self.sbox.append ( "northern flicker" )
        self.sbox.append ( "ovenbird" )
        self.sbox.append ( "parula" )
        print "Size is now", self.sbox.count()

4.6. Main program

This is the usual main program for a Tkinter application: instantiate an Application object, set up the application's title in the window frame, and wait for events.

scrolledlisttest
#================================================================
# Main program
#----------------------------------------------------------------

app  =  Application()
app.master.title ( "scrolledlisttest" )
app.mainloop()