Next / Previous / Contents / Shipman's homepage

15. A small test driver

Here is a small application to demonstrate the PageTurner widget. It has only two widgets: a PageTurner and a Quit button.

pageturnertest
#!/usr/bin/env python
#================================================================
# pageturnertest:  Test driver for the PageTurner widget.
#   Do not edit this file.  It is extracted automatically from its
#   documentation file.  See:
#     http://www.nmt.edu/~shipman/soft/pageturner/
#----------------------------------------------------------------

We import the Tkinter page as usual, then import all the functions of the widget under test.

pageturnertest
from Tkinter import *
from pageturner import *

The Application class is the base class for the GUI application.

pageturnertest
# - - - - -   c l a s s   A p p l i c a t i o n   - - - - -

class Application(Frame):

The constructor starts by calling its parent constructor and then calling a .__createWidgets() method to place all its widgets.

pageturnertest
# - - -   A p p l i c a t i o n . _ _ i n i t _ _   - - -

    def __init__ ( self, master=None ):
        Frame.__init__(self, master)
        self.grid()
        self.__createWidgets()

The .__createWidgets() method starts by instantiating a PageTurner widget and gridding it, then adding the usual Quit button.

pageturnertest
# - - -   A p p l i c a t i o n . _ _ c r e a t e W i d g e t s   - - -

    def __createWidgets(self):
        """Place all widgets in self."""
        self.pager  =  PageTurner ( self, size=(500,300) )
        self.pager.grid ( row=0, column=0 )
        self.quitButton  =  Button ( self, text="Quit",
            command=self.quit )
        self.quitButton.grid ( row=99, column=0, columnspan=99,
            sticky=E+W )

The content pages to be added to the PageTurner are very simple. Each consists of a single Label widget containing the string “This is page...”.

pageturnertest
        for i in range(1,9):
            content  =  Frame ( self.pager.bodyFrame )
            lab = Label ( content, text="This is page %d." % i )
            lab.grid(row=0, column=0, sticky=NW)
            self.pager.addPage ( content )

The main is the usual Tkinter main, instantiating the Application widget, setting its window name, and then entering the main loop.

pageturnertest
# - - - - -   m a i n   - - - - -

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