From: gmcm at hypernet.com (Gordon McMillan)
Date: Tue, 20 Apr 1999 03:57:28 GMT
Subject: accessing calling scripts from called scripts at run time
In-Reply-To: <gaQS2.5877$s_5.291559@dfiatx1-snr1>
References: <gaQS2.5877$s_5.291559@dfiatx1-snr1>
Message-ID: <1287541644-22552815@hypernet.com>
Content-Length: 1195
X-UID: 539                                                  

John Michelsen says:

> I'm having trouble accessing a calling script from a called script
> at run time. Here is an example:
> 
> #first file, will call second
> from Tkinter import *
> import tkFileDialog, string
> 
> root = None
> 
> def importScript():
>     filename = tkFileDialog.askopenfilename()
>     if filename != '':
>         list = string.split(filename, '/')
>         filename = list[-1]
>         #dir = string.join(list[:-1], '/')
>         #sys.path.append(dir)
>         exec "import %s" % filename[:-3]
> 
> if __name__ == "__main__":     #?
>     root = Tk()
>     b = Button(root, text="import script", command=importScript)
>     b.pack() root.mainloop()
> 
> 
> #another file, picked by tkFileDialog at run time by user
> from importTest import root
> root.config(bg='green')
> 
> 
> If I put the "if __name__ == '__main__':"  in, the second file can't
> get at the root properly. If I leave it out, when I import root in
> the second file, it creates a new root window.  How can I get at the
> root without making a new one?
> 

The straightforward way: Give all the dialog-modules a run(...) 
method. Use
 mod = __import__(filename)
 mod.run(root, ....)

- Gordon




