[Tutor] More Tkinter Help Please [callbacks and embedded functions]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 17 Jun 2002 13:44:40 -0700 (PDT)


> > The solution actually isn't too bad:
> >
> > ###
> >       def saving_callback(): Saving(f)
> >       self.b4 = Button(f, text="Save Input", command=saving_callback)
> > ###
> >
> > That is, we can actually embed a small function called
> > 'saving_callback', and we can pass that off to as the button callback.
> > The miniature function has access to all the local variables of its
> > parent, which is why saving_callback() can say 'f' without problems.
> >
>
>
> Ok. Now I get a different error:
>
> Exception in Tkinter callback
> Traceback (most recent call last):
>   File "/sw/src/root-python-2.2.1-6/sw/lib/python2.2/lib-tk/Tkinter.py",
> line 1292, in __call__
>     return apply(self.func, args)
> TypeError: saving_callback() takes no arguments (1 given)


Hmmm!  When does the error occur?  Also, can you show us the source code
of that Dialog box again?  I just want to check something quick.  The
error message doesn't make too much sense to me yet, because button
command callbacks shouldn't be sending any arguments over to us.

(Tkinter "event" callbacks, on the other hand, send off an 'event' object,
and are used for things like keyboard input.)




> Also where does the f come from? Is it from the f = Frame(top) in the
> __init__ portion of the PyShell calss? If so, how would this carry over
> the saving_callback function?

>From Python 2.1 onward, inner functions are allowed to carry the variables
of the outer function.  If you're running Python 2.1, you may need the
line:

###
from __future__ import nested_scopes
###

to get this to kick in, but in Python 2.2, this nesting functionality
comes for free.

Since saving_callback() is defined with the __init__ of our PyShell class,
it gets access to the local variables of PyShell.__init__, including that
'f' variable.



> Would the Dialog Box be separate from the initial frame?

That was my assumption --- I thought that dialog boxes open up a new
window.  Dialogs also prevent interactivity with the parent window until
the dialog window itself is closed, I think.


Good luck to you!