Saving input from entry box into .txt files

Multi tool use
Multi tool use


Saving input from entry box into .txt files



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.


.txt



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()





Possible duplicate of Why are multiple instances of Tk discouraged?
– Lafexlos
Jul 2 at 10:54





Changing "top = tk.Tk()" to "top = tk.Toplevel()" works (which is basically a summary of Lafexlos' link).
– user1729
Jul 2 at 11:08





3 Answers
3



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.


file_name


e


pack()


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.





It works!! Thanks for your help!!
– jk le
Jul 2 at 20:47


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)


t = tk.StringVar()


t = tk.StringVar(top)


pack()


None





You can't have two Tk instances.
– Lafexlos
Jul 2 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 2 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 2 at 11:12






Well yeah, can't is a strong word, my bad on that.
– Lafexlos
Jul 2 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 2 at 11:29



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


save()



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()





See: tk.Button(top, text = 'Save as a file', command = save).pack()
– jedwards
Jul 2 at 11:02


tk.Button(top, text = 'Save as a file', command = save).pack()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

1RvKnnZfc3JO,KfWAAsYOia3yJSb1FvIav,OiYDs0EI8OW0S5SF,Y,tTJEihI3GPQUibzEG TXUvoYzkTP3U,xt c,Z
8B vSGP9m,2RuAawkbklSkV43Ypu

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications