Using other .py scipts within a script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Using other .py scipts within a script?

    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:

    Code:
    from Tkinter import *
    import OtherScript
    
    def NewWindow():
        win = Toplevel()
        Run OtherScript.py
    
    root = Tk()
    root.geometry('300x300')
    
    Button(root, text='Click Me', command=NewWindow).pack(side=BOTTOM)
    
    mainloop()
    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:

    Code:
    if __name__ == "__main__":
        import sys
  • Thekid
    New Member
    • Feb 2007
    • 145

    #2
    After playing around with this, I think I've figured it out:

    Code:
    #OtherScript.py
    
    from Tkinter import *
    
    root = Tk()
    root.geometry('300x300')
    
    Button(root, text='Exit', command=root.destroy).pack()
    
    if __name__ == "__main__":
        mainloop()
    Then the root window with OtherScript imported:

    Code:
    from Tkinter import *
    
    def NewWindow():
        import tknw
    
    root = Tk()
    
    root.geometry('300x300')
    
    Button(root, text='Click Me', command=NewWindow).pack(side=BOTTOM)
    
    mainloop()
    This seems to work but if it shouldn't be done this way, please let me know. Thanks

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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.

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        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.

        Code:
        from Tkinter import *
        import OtherScript
        
        
        root = Tk()
         
        root.geometry('300x300')
         
        Button(root, text='Click Me', command=None).pack(side=BOTTOM)
         
        mainloop()
        Doing that ^ will result in the root window AND the OtherScript running simultaneously.
        Last edited by Thekid; Oct 4 '10, 12:39 AM. Reason: Added code example

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          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:
          Code:
          # module1.py
          import Tkinter
          
          class LabelWidget(Tkinter.Entry):
              def __init__(self, master, x, y, text):
                  self.text = Tkinter.StringVar(master)
          
          class EntryWidget(Tkinter.Entry):
              def __init__(self, master, x, y, v):
                  Tkinter.Entry.__init__(self, master=master)
          
          class EntryGrid(Frame):
              ''' SDS/2 Frame with Tkinter.Entry widgets arranged in columns and rows.'''
              def __init__(self, parent, obj, cols, rows, pattern, anchorPattVar):
                  pass
          
          class EmbedDialog(object):
              
              def __init__(self, model):
                  pass
          
          if __name__ == "__main__":
              class Model:
                  def __init__(self, **kw):
                      for key in kw:
                          setattr(self, key, kw[key])
              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:
          Code:
          import module1
          class Model:
              def __init__(self, **kw):
                  for key in kw:
                      setattr(self, key, kw[key])
          dlg = module1.EmbedDialog(Model(x=12, y=24))

          Comment

          Working...