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.
#!/usr/bin/env python #================================================================ #scrolledlisttest: Test driver forScrolledList# # For documentation, see: # http://www.nmt.edu/tcc/help/lang/python/examples/scrolledlist/ #---------------------------------------------------------------- from Tkinter import * import scrolledlist
Next we start declaring the Application
class, which embodies the entire graphical user
interface.
class Application(Frame):
"""GUI for scrolledlisttest
"""
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.
def __init__ ( self ):
"""Constructor for the Application class.
"""
Frame.__init__ ( self, None )
self.grid()
self.__createWidgets()
self.__tests()
There are only two widgets: the ScrolledList widget we are
testing, and a quit button below it.
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 )
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.
def __pickHandler ( self, linex ):
"""Handler for user clicks on lines in the listbox.
"""
print "Click on line %d, '%s'" % (linex, self.sbox[linex])
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.
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.
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.
print "Clear listbox:"
self.sbox.clear()
print "Size is now", self.sbox.count()
We add the same three lines back in, then test insertion.
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.
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.
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()
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.
#================================================================
# Main program
#----------------------------------------------------------------
app = Application()
app.master.title ( "scrolledlisttest" )
app.mainloop()