[Tutor] return a dictionary?
Alan Gauld
alan.gauld at btinternet.com
Thu Oct 9 21:56:50 CEST 2008
"Bill Gibson" <wbgibson2 at yahoo.com> wrote
> Can I ‘return’ a dictionary from an imported Python script?
Yes.
> I have a utility module (NameAddress.py) done using
> Tkinter. ...I wanted to return the values as a dictionary
> to the module that imported but this does not seem to work.
I'm not totally clear what you mean here.
> This is the dictionary as printed out from the
> NameAddress.py by
> print theDict
> shows what I want:
Is the print statement in your module or in NameAddress.py?
> However the code for return in NameAddress.py , I.E.
> return theDict
> gives this error when called from a testing module
> (Inter_module.py) whose code looks like this:
> import NameAddress
> y = NameAddress.getName()
> print y
> Traceback (most recent call last):
> File "C:\Users\B\Desktop\Inter_module.py", line 6, in ?
> y = NameAddress.getName()
> File "C:\Users\B\Desktop\NameAddress.py", line 22, in getName
> theDict={'Name': w2.get(), 'Address1': w4.get(), ...
> File "C:\Python24\Lib\lib-tk\Tkinter.py", line 2303, in get
> return self.tk.call(self._w, 'get')
> TclError: invalid command name ".43065216"
This suggests an invalid reference is being sent to the method.
To me it doesn't look like you create any data or instances
or anything with NameAddress? I assume it does all of that
in its initialisation when you import it?
Othewise you are going to have to do something to initialise
some Tkinter widgets before trying to access them via getName().
Are there any data attributes you can access directly in
the module to prove it has initialised?
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list