title: Python gui - passing input to script
body: <p>I currently have a main python script (<code>main.py</code>) which reads input from a second script (<code>input.py</code>) which can be modified by a user. The user sets variables such as number of dimensions (<code>ndim</code>), number of points (<code>npts</code>) etc. in the second script and these are read into <code>main.py</code> using the following:</p>
  
  <pre><code>filename = sys.argv[-1]    
  m = __import__(filename)
  
        ndim = m.ndim
        npts1 = m.npts1
        npts2_recorded = m.npts2_recorded
  </code></pre>
  
  <p>The script is executed by the following command:</p>
  
  <p><code>python main.py input</code></p>
  
  <p>I would like to replace <code>input.py</code> with a GUI. Tkinter seems a sensible place to start and I can see how to create a GUI to enable the user to set the various options that they would otherwise have set in <code>input.py</code>. However, I do not know how to pass this information to <code>main.py</code> from the GUI. Is there an equivalent to <code>__import(filename)__</code> which can extract information from selections made by a user in the GUI, or is there another way of achieving the same effect.</p>
  
  <p><strong>A minimal (not) working example based on the answer below:</strong> 
  This code creates the file <code>example.txt</code> but the text given to <code>block1</code> does not get written to the file.</p>
  
  <pre><code>from Tkinter import *
  
  def saveCallback():
    with open("example.txt",'w') as outfile:
      outfile.write(block1.get())
  
  def UserInput(status,name):
    optionFrame = Frame(root)
    optionLabel = Label(optionFrame)
    optionLabel["text"] = name
    optionLabel.pack(side=LEFT)
    var = StringVar(root)
    var.set(status)
    w = Entry(optionFrame, textvariable= var)
    w.pack(side = LEFT)
    optionFrame.pack()
    return w
  
  if __name__ == '__main__':
    root = Tk()
  
    block1 = UserInput("", "Block size, dimension 1")
  
    Save_input_button = Button(root, text = 'Save input options', command = saveCallback())
    Save_input_button.pack()
    root.mainloop()
  </code></pre>