0

I want to save the input form the entry box into .txt files and it works on the first code and not the second code, but what I need to use is the second code.

Code 1:

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 2:

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()
jk le
  • 19
  • 1
  • 2
  • 1
    Possible duplicate of [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – Lafexlos Jul 02 '18 at 10:54
  • Changing "top = tk.Tk()" to "top = tk.Toplevel()" works (which is basically a summary of Lafexlos' link). – user1729 Jul 02 '18 at 11:08

3 Answers3

1

You are confusing your variable with the entry box: using better variable names helps. You are also writing the file_name in the file you are creating with this name... It is unclear if it is really what you wanted. You are also packing on the same line as assigning to variable e - pack() returns None
For some reason, you also launched two mainloops; don't do this, it is a bad idea.

import tkinter as tk

def save():
    file_name = entry.get()
    with open(file_name + '.txt', 'w') as file_object:
        file_object.write(file_name)   # it is unclear if writing the file_name in the newly created file is really what you want.

if __name__ == '__main__':
    top = tk.Tk()
    entry_field_variable = tk.StringVar()
    entry = tk.Entry(top, textvariable=entry_field_variable)
    entry.pack()
    tk.Button(top, text="save", command=save).pack()

    top.mainloop()

I removed the nested functions; if you feel compelled to do this, maybe you should use a class instead.
I also changed the opening/closing of the file to a context manager that handles it for you.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0
import tkinter as tk

def f():
    def save():
        a = t.get()
        with open((a + '.txt'), 'w') as f: 
            f.write(a)

    top = tk.Tk()
    t = tk.StringVar(top)
    tk.Entry(top, textvariable=t).pack()
    tk.Button(top, text = 'Save as a file', command=save).pack()

root = tk.Tk()
tk.Button(root, text="Button", command=f).pack()
root.mainloop()

The most important change is t = tk.StringVar() -> t = tk.StringVar(top), specifying the master widget. There are a few other changes (e.g. pack() returns None so don't set values based on it, use a context manager for your file closing)

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • You can't have two Tk instances. – Lafexlos Jul 02 '18 at 11:09
  • @Lafexlos Of course you can, *you probably shouldn't*, because there are probably better ways, but the code clearly works. (try it) – jedwards Jul 02 '18 at 11:10
  • @Lafexlos did you read the suggested duplicate that *you* linked? *"From a technical standpoint, there's no reason why you can't have two instances of Tk at the same time."* – jedwards Jul 02 '18 at 11:12
  • Well yeah, _can't_ is a strong word, my bad on that. – Lafexlos Jul 02 '18 at 11:16
  • @jkle, If you accepted my answer because it's closer to yours than the others, that's mostly because I put less thought into it than others. You should consider marking the answer from ReblochonMasque as correct. Even if you decide to use this approach, other readers may have interest in a more general solution. – jedwards Jul 02 '18 at 11:29
-2

You are not calling save() function in the second code. Adding save() call after definition would solve your problem.

Your code:

def f():
    def save():
        a = t.get()
        f = open((a + '.txt'), 'w')
        f.write(a)
        f.close()
        return

fixed:

def f():
    def save():
        a = t.get()
        f = open((a + '.txt'), 'w')
        f.write(a)
        f.close()
        return
    save()
wizofwor
  • 917
  • 8
  • 25