[Tutor] Request for Comments (original) (raw)

Michael Lange klappnase at freenet.de
Thu Jul 29 12:11:57 CEST 2004


On Tue, 27 Jul 2004 11:23:16 -0700 "Faulconer, Steven M." <STEVEN.M.FAULCONER at saic.com> wrote:

I'd like to have a better way of creating the gui elements. Since there are currently 5 checkers, I've got a lengthy bit of code to create the widgets. It would be nice to have a method that loops over a data structure (dictionary/list) and builds the gui elements from the contents. I'll have to work on that for the next version. Hi, Steven,

you currently have five methods in your CheckerApp class that look all the same; I think you might replace these with a single method like this:

def RunChecker(self, checkerName): if checker in self.checkerNames:# with self.checkerNames as a list that contains the possible checkers # Build the commandline sequence command = self.SSBIN + self.BINPATH + checkerName + self.PROJPATH +
self.PROJECT + ".prj" + " " + path.joinpath( self.DBPATH, self.DATABASE )

    # Build the path to the report file
    report = self.DBPATH.joinpath( self.DATABASE ) + ".cnt"

    # If the report file exists, remove it. The creation of the report file
    # is used to determine if the program ran correctly.
    if exists( report ):
        remove( report )

    # Run the checker command
    CheckerWindow( self.newroot, self, checkerName, command, report )

The same should be possible for creating the gui elements if you add a class that contains all the elements you need for the five checkers:

class CheckerGroup(Tkinter.Frame): def init(self, master, checkerName, **kw): Tkinter.Frame.init(self, master, **kw)

    # replace attributes of the CheckerApp class with something appropriate in the code below

    self.fnd_dup_group = Pmw.Group( master, tag_text = title )
    self.fnd_dup_group.pack( pady = 2, padx = 2, expand = 'yes', fill = 'both' )
    self.fnd_dup_run = Tkinter.StringVar()

    if self.OPTIONS['find_dup_feat'][ 0 ] == "":
        self.fnd_dup_run.set( "Last ran on : Never" )
    else:
        self.fnd_dup_run.set( "Last ran on : " + self.OPTIONS['find_dup_feat'][ 0 ] )
    Tkinter.Label( self.fnd_dup_group.interior(),
                   textvariable = self.fnd_dup_run ).pack( anchor = Tkinter.W )
    self.fnd_dup_btns = Pmw.ButtonBox( self.fnd_dup_group.interior() )
    self.fnd_dup_btns.pack( fill = 'both', expand = 'yes', padx = 5, pady = 5 )
    self.fnd_dup_btns.add( 'Run Checker',
                             command = lambda
                             arg1 = 'find_dup_feat':
                             self.RunChecker( arg1 ) )
    self.fnd_dup_btns.add( 'View Report', command = lambda
                                          arg1 = "find_dup_feat":
                                          self.ViewReport( arg1 ) )

then you could write in the CheckerApp class something like:

self.checkers = {}# collect the CheckerGroups in a dictionary to keep a reference for checker in self.checkerNames: newchecker = CheckerGroup(self.newroot, checker) newchecker.pack() self.checkers[checker] = newchecker

(now that I've written this I think that maybe a dictionary might be better than a list for self.checkerNames, and maybe the RunChecker() method should be an attribute of the CheckerGroup class rather than one of CheckerApp; anyway, I guess you'll see my point.)

I hope this helps

Michael



More information about the Tutor mailing list