title: Saving input from entry box into .txt files
body: <p>I want to save the input form the entry box into <code>.txt</code> files and it works on the first code and not the second code, but what I need to use is the second code.</p>
  
  <p>Code 1:</p>
  
  <pre><code>import tkinter as tk
  
  def f():
      def save():
          a = t.get()
          f = open((a + '.txt'), 'w')
          f.write(a)
          f.close()
          return
  
     top = tk.Tk()
     t = tk.StringVar()
     e = tk.Entry(top, textvariable = t).pack()
     b = tk.Button(top, text = 'Save as a file', command = save).pack()
     top.mainloop()
  f()
  </code></pre>
  
  <p>Code 2:</p>
  
  <pre><code>import tkinter as tk
  
  root = tk.Tk()
  
  def f():
      def save():
          a = t.get()
          f = open((a + '.txt'), 'w')
          f.write(a)
          f.close()
          return
  
      top = tk.Tk()
      t = tk.StringVar()
      e = tk.Entry(top, textvariable = t).pack()
      b = tk.Button(top, text = 'Save as a file', command = save).pack()
      top.mainloop()
  
  button = tk.Button(root, text="Button",command=f).pack()
  root.mainloop()
  </code></pre>