[Tutor] More Tkinter Help Please [Dialog windows]

alan.gauld@bt.com alan.gauld@bt.com
Wed, 19 Jun 2002 10:29:11 +0100


First, I saw your message where you got it working - good, 
however there are some concepts obviously not quite 
clear yet so here is an attempt to pick up the 
loose ends....

> >    def doSave(self):        # inside Pyshell class
> 
>     I can't use self in "def doSave(self):" because the 
> doSave function is actually inside PyShell.__iniT__. 

You use self when defining the method at the class level.
You have chosen(as Danny suggested) to put the event handler 
inside init. Thats OK and you don't need the self parameter.

I suggeted putting the event handler at the class level, 
in which case you do neeed self. Its really a matter of 
personal preference.

> If I use self and place this function
> inside PyShell but outside of __init__ I get an error 
> claiming the function is asking for on arument but none 
> are given. 

Nope, you got that error when you put the method in the class 
but *did not* use self. It complained because the class passed 
it the object as self but the function didn't expect it!

class C:
  def ok(self):
    print 'ok'
  def bad():  # no self defined
    print 'oops!'
  def master(self):
    self.bad()  # class calls C.bad(self) get error message

c = C()
c.ok()
c.master()  # error here


> If I place the function inside
> __init__ everything works except for the 
> "self.saveText(filename)" portion
> because there is no instance of self inside the function?

Correct, that's why I prefer event handlers to be defined 
at the class level. This is about 'scoping' or 'namespaces'.

Inside a *function* (including one defined inside a method)
you can see local variables, parameters and module level 
names but not class level names. Inside a method you can see
the same but since one of the parameters is selfyou thus get 
access to the object attributes.

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld