accessing calling scripts from called scripts at run time
Gordon McMillan
gmcm at hypernet.com
Mon Apr 19 23:57:28 EDT 1999
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
More information about the Python-list
mailing list