accessing calling scripts from called scripts at run time

John Michelsen john.michelsen at gte.net
Mon Apr 19 20:49:16 EDT 1999


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?

Thanks,

John







More information about the Python-list mailing list