By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Using other .py scipts within a script?

Thekid
100+
P: 145
I'm currently working on a tkinter project and the code is getting pretty long. What I would like to do is separate some of it into .py scripts and then import them into the main script. I'm not sure how to get a separate script to work like this. My example is a tkinter window that has many toplevel windows that have a lot of code in them. I'd like to keep those separate if possible. Something like this:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import OtherScript
  3.  
  4. def NewWindow():
  5.     win = Toplevel()
  6.     Run OtherScript.py
  7.  
  8. root = Tk()
  9. root.geometry('300x300')
  10.  
  11. Button(root, text='Click Me', command=NewWindow).pack(side=BOTTOM)
  12.  
  13. mainloop()
  14.  
  15.  
Instead of having all of the code from OtherScript in here, I'd like to import it and have it run after the root window button is clicked.

At the bottom of 'OtherScript.py' I've added:

Expand|Select|Wrap|Line Numbers
  1. if __name__ == "__main__":
  2.     import sys
  3.  
Oct 2 '10 #1
Share this Question
Share on Google+
4 Replies


Thekid
100+
P: 145
After playing around with this, I think I've figured it out:

Expand|Select|Wrap|Line Numbers
  1. #OtherScript.py
  2.  
  3. from Tkinter import *
  4.  
  5. root = Tk()
  6. root.geometry('300x300')
  7.  
  8. Button(root, text='Exit', command=root.destroy).pack()
  9.  
  10. if __name__ == "__main__":
  11.     mainloop()
  12.  
Then the root window with OtherScript imported:

Expand|Select|Wrap|Line Numbers
  1.  
  2. from Tkinter import *
  3.  
  4. def NewWindow():
  5.     import tknw
  6.  
  7. root = Tk()
  8.  
  9. root.geometry('300x300')
  10.  
  11. Button(root, text='Click Me', command=NewWindow).pack(side=BOTTOM)
  12.  
  13. mainloop()
  14.  
This seems to work but if it shouldn't be done this way, please let me know. Thanks
Oct 3 '10 #2

bvdet
Expert Mod 2.5K+
P: 2,851
I think all imports should be at the top of the main application file. See Style Guide for Python Code and look for the section on imports.
Oct 3 '10 #3

Thekid
100+
P: 145
bvdet, that's what I initially tried but as soon as I'd run the root window, the imported one would open at the same time.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import OtherScript
  3.  
  4.  
  5. root = Tk()
  6.  
  7. root.geometry('300x300')
  8.  
  9. Button(root, text='Click Me', command=None).pack(side=BOTTOM)
  10.  
  11. mainloop()
  12.  
  13.  
Doing that ^ will result in the root window AND the OtherScript running simultaneously.
Oct 3 '10 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Encapsulate the code in the imported modules in functions or classes and call the functions or instantiate the classes in the main script.

A code example of a script to import:
Expand|Select|Wrap|Line Numbers
  1. # module1.py
  2. import Tkinter
  3.  
  4. class LabelWidget(Tkinter.Entry):
  5.     def __init__(self, master, x, y, text):
  6.         self.text = Tkinter.StringVar(master)
  7.  
  8. class EntryWidget(Tkinter.Entry):
  9.     def __init__(self, master, x, y, v):
  10.         Tkinter.Entry.__init__(self, master=master)
  11.  
  12. class EntryGrid(Frame):
  13.     ''' SDS/2 Frame with Tkinter.Entry widgets arranged in columns and rows.'''
  14.     def __init__(self, parent, obj, cols, rows, pattern, anchorPattVar):
  15.         pass
  16.  
  17. class EmbedDialog(object):
  18.  
  19.     def __init__(self, model):
  20.         pass
  21.  
  22. if __name__ == "__main__":
  23.     class Model:
  24.         def __init__(self, **kw):
  25.             for key in kw:
  26.                 setattr(self, key, kw[key])
  27.     x = EmbedDialog(Model(x=12, y=24))
The code following the if statement does not execute when the module is imported, but is executed when the file is run as a script.

The main script:
Expand|Select|Wrap|Line Numbers
  1. import module1
  2. class Model:
  3.     def __init__(self, **kw):
  4.         for key in kw:
  5.             setattr(self, key, kw[key])
  6. dlg = module1.EmbedDialog(Model(x=12, y=24))
Oct 4 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.