nest class name problem
Bob van der Poel
bvdpoel at uniserve.com
Tue Jun 20 18:45:50 EDT 2000
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.
class Goption:
__searchName = ""
__optDefault = ""
__optSetting = ""
def __init__(self, title, setting ):
self.__searchName=title
self.__optDefault=setting
self.__optSetting=setting
def get(self):
return self.__optSetting
def change(self):
class MyDialog(tkSimpleDialog.Dialog):
def body(self, master):
Label(master, text="%s Options:" %
Goption._Goption__searchName).grid(row=0)
self.e1 = Entry(master)
self.e1.insert(0,
Goption._Goption__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,
Goption._Goption__optDefault)
def apply(self):
self.result = self.e1.get()
a = MyDialog(root, title="FindSong %s Options" %
self.__searchName)
if a.result:
self.__optSetting = a.result
The Goption._Goption_??? calls are from another members suggestions. But
no joy...Inserting some print lines shows that
Goption._Goption_optSetting are
undefinded. So:
Am I on the right track...or is my concept of how to do this just plain
dumb?
Am I missing something really obvious on how to get the values I need?
--
__
/ ) / Bob van der Poel
/--< ____/__ bvdpoel at uniserve.com
/___/_(_) /_) http://users.uniserve.com/~bvdpoel
More information about the Python-list
mailing list