[Tutor] More Tkinter Help Please [callbacks and embedded
functions]
SA
sarmstrong13@mac.com
Mon, 17 Jun 2002 15:22:25 -0500
On 6/16/02 1:33 PM, "Danny Yoo" <dyoo@hkn.eecs.berkeley.edu> wrote:
>
> I'm not too familiar with tkSimpleDialog.Dialog, so I'm not quite sure if
> there's a problem with inheritance. Can you explain more what problems
> you're running into with this code?
>
>
> Ah! I think that for this statement:
>
> self.b4 = Button(f, text="Save Input", command=Saving)
>
>
> if 'tkSimpleDialog' is the tkSimpleDialog that's in the 'Introduction To
> Tkinter' tutorial:
>
> http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm
>
>
> then we need to do a little extra: the dialog box needs to know its parent
> window when it's being constructed! Let's take a look at the definition
> of tkSimpleDialog.Dialog again:
>
>
> ### Sample of tkSimpleDialog.py
> # File: tkSimpleDialog.py
>
> from Tkinter import *
> import os
>
> class Dialog(Toplevel):
>
> def __init__(self, parent, title = None):
> ###
>
>
> So when we create a Dialog box, this dialog box must take in an additional
> 'parent' parameter. However, back in your code:
>
> self.b4 = Button(f, text="Save Input", command=Saving)
>
> When the fourth button is pressed, it tries to call the Saving dialog
> constructor with no parameters: this may be the problem that you're
> running into. We'd like to be able to do something like:
>
> self.b4 = Button(f, text="Save Input", command=Saving(f))
>
> since 'f' is the frame parent that we'd like to attach the Dialog to...
> but the problem with this is that Python will actually call Saving(f). It
> calls it too soon, because, to Python,
>
> Saving(f)
>
> is a function call, so it just evaluates it straightaway. We need a way
> to create a callback function that knows about 'f', enough so that it can
> do a 'Saving(f)' when our button is pressed. What to do?
>
>
>
>
>
> 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)
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? Would the Dialog Box be separate from the initial
frame?
Thanks in advance.
SA