nest class name problem

Michael Hudson mwh21 at cam.ac.uk
Tue Jun 20 19:15:40 EDT 2000


Bob van der Poel <bvdpoel at uniserve.com> writes:

> Michael Hudson wrote:
> > 
> > Bob van der Poel <bvdpoel at uniserve.com> writes:
> > 
> > > Yes, but I'm using __name to keep the variable private/static to a
> > > particular instance of the Goption().
> > 
> > This is a bit of a wild guess but are you suffering from
> > instance/class confusion?
> > 
> > Cheers,
> > M.
> 
> More than likely.... Perhaps I should have made my example and
> reason more clear.... I have a simple (tkinter) program which does some
> datafile searches using either grep or agrep. I thought it'd be nice to
> be able to change the options fo grep/agrep so I wrote a little function
> to do this. Problem with this is that I ended up with 2 nearly identical
> functions (one for grep, another for agrep); and I found that I needed
> some global variables to store the current and default settings.
> 
> so, being a newbie to the wonderful world of classes, objects and other
> things I don't understand I figured that I'd do this as a class. To
> create the class I do:
>         grep_opts = Goption('-ih', "Grep")
> 
> which creates a class instance. I can (hopefully) now do the following:
> 
>         grep_opts.get() to get the current setting
>         grep_opts.change() to call the gui to change the current
> setting.
> 
> So, here's the simple thingie I came up with. Note, that this uses the
> dialog class from tkinter to create the dialog window (and this is where
> my problems come in.

Hmm.  I know next to nothing about Tkinter, but here's how I'd do
something a bit like this.  Spread "IMHO"s liberally around what
follows :-)

Firstly, don't bother nesting the class MyDialog inside Goption.  It's
just not worth the hassle.

class MyDialog(tkSimpleDialog.Dialog):
    def __init__(self,gopt,**kw):
        self.gopt = gopt
        # this is warty:
        apply(tkSimpleDialog.Dialog.__init__,(self,),kw)

    def body(self, master):
        Label(master, text="%s Options:" % self.gopt.searchName).grid(row=0)
        self.e1 = Entry(master)
        self.e1.insert(0, self.gopt.optSetting)
        self.e2 = Button(master, text='Default',
                         command=self.default)
        self.e1.grid(row=0, column=1)
        self.e2.grid(row=0, column=2)
        return self.e1 # initial focus

    def default(self):
        self.e1.delete(0,END)
        self.e1.insert(0, self.gopt.optDefault)

    def apply(self):
        self.result = self.e1.get()

class Goption:
   def __init__(self, title, setting):
        self.searchName = title
        self.optDefault = self.optSetting = setting
   def get(self):
        return self.optSetting
   def change(self):
        a = MyDialog(parent=root,gopt=self,
                     title="FindSong %s Options" %self.searchName)
        if a.result:
            self.optSetting = a.result
        
> Am I on the right track...or is my concept of how to do this just plain
> dumb?

I think you're on the right track, but perhaps not quite sure how to
get down it.

I might be able to help more if I knew how Tkinter worked...
 
> Am I missing something really obvious on how to get the values I
> need?

I don't think so.  I hope this helped!

Cheers,
M.

-- 
  Indeed, when I design my killer language, the identifiers "foo" and
  "bar" will be reserved words, never used, and not even mentioned in
  the reference manual. Any program using one will simply dump core
  without comment. Multitudes will rejoice. -- Tim Peters, 29 Apr 1998



More information about the Python-list mailing list