Saving a variable in a text file
You have to know the variable's name at compilation time. So all you need to do is:
with open('some_file.txt', 'w') as f:
f.write("balance %d" % balance)
This can be more convenient to manage using a dict
object for mapping names to values.
You may also want to read about the pickle
or json
modules which provide easy serialization of objects such as dict
.
The way to use a serializer such as pickle
is:
import pickle as serializer
balance = total_savings - total_expenses
with open('some_file.txt', 'w') as f:
serializer.dump( balance, f)
You can change pickle
to json
in the provided code to use the other standard serializer and store objects in json
format.
Edit:
In your example you're trying to store text from tkinter
's Entry
widget. Read about it here.
What you probably miss is using a StringVariable to capture the entered text:
Create StringVar
for variables:
username = StringVar()
password = StringVar()
Register StringVar variables to Entry widgets:
e1 = Entry (register, textvariable=username)
e2 = Entry (register, textvariable=password, show= "*")
Save content using StringVar
in two seperate files:
import json as serializer
with open('godhelpme.txt', 'w') as f:
serializer.dump(username.get(), f)
with open('some_file.txt', 'w') as f:
serializer.dump(password.get(), f)
If you want them in the same file create a mapping (dict
) and store it:
import json as serializer
with open('godhelpme.txt', 'w') as f:
serializer.dump(
{
"username": username.get(),
"password": password.get()
}, f
)
Edit 2:
You were using the serialization before entering text. Register a save
function (that can later exit) to the register button. This way it will be called after the user clicked it (that means the content is already there). Here is how:
from tkinter import *
def save():
import json as serializer
with open('godhelpme.txt', 'w') as f:
serializer.dump(username.get(), f)
with open('some_file.txt', 'w') as f:
serializer.dump(password.get(), f)
register.quit()
register = Tk()
Label(register, text ="Username").grid(row = 0)
Label(register, text ="Password").grid(row = 1)
username = StringVar()
password = StringVar()
e1 = Entry (register, textvariable=username)
e2 = Entry (register, textvariable=password, show= "*")
e1.grid(row = 0, column = 1)
e2.grid(row = 1, column = 1)
# changed "command"
button1 = Button(register, text = "Register", command = save)
button1.grid(columnspan = 2)
button1.bind("<Button-1>")
register.mainloop()
What happened before was the save-to-file process happened immediately before the user inserts any data. By registering a function to the button click you can ensure that only when the button is pressed, the function executes.
I strongly suggest you play with your old code in a debug environment or use some prints to figure out how the code works.
honeybadger
Updated on May 10, 2020Comments
-
honeybadger almost 4 years
I would like to save variable (including its values) into a text file, so that the next time my program is opened, any changes will be automatically saved into the text file .For example:
balance = total_savings - total_expenses
How would I go about saving the variable itself into a text file instead of only its value? This section is for the register page
from tkinter import * register = Tk() Label(register, text ="Username").grid(row = 0) Label(register, text ="Password").grid(row = 1) e1 = Entry (register) e2 = Entry (register, show= "*") e1.grid(row = 0, column = 1) e2.grid(row = 1, column = 1) username = e1.get() password = e2.get() button1 = Button(register, text = "Register", command = register.quit) button1.grid(columnspan = 2) button1.bind("<Button-1>") import json as serializer with open('godhelpme.txt', 'w') as f: serializer.dump(username, f) with open('some_file.txt', 'w') as f: serializer.dump(password, f) register.mainloop()
Altered code:
from tkinter import * register = Tk() Label(register, text ="Username").grid(row = 0) Label(register, text ="Password").grid(row = 1) username = StringVar() password = StringVar() e1 = Entry (register, textvariable=username) e2 = Entry (register, textvariable=password, show= "*") e1.grid(row = 0, column = 1) e2.grid(row = 1, column = 1) button1 = Button(register, text = "Register", command = register.quit) button1.grid(columnspan = 2) button1.bind("<Button-1>") import json as serializer with open('godhelpme.txt', 'w') as f: serializer.dump(username.get(), f) with open('some_file.txt', 'w') as f: serializer.dump(password.get(), f)
Log in code:
from tkinter import * login = Tk() Label(login, text ="Username").grid(row = 0) Label(login, text ="Password").grid(row = 1) username = StringVar() password = StringVar() i1 = Entry(login, textvariable=username) i2 = Entry(login, textvariable=password, show = "*") i1.grid(row = 0, column = 1) i2.grid(row = 1, column = 1) def clickLogin(): import json as serializer f = open('godhelpme.txt', 'r') file = open('some_file.txt', 'r') if username == serializer.load(f): print ("hi") else: print ("invalid username") if password == serializer.load(file): print ("HELLOOOO") else: print ("invalid password") button2 = Button(login, text = "Log In", command = clickLogin) button2.grid(columnspan = 2) login.mainloop()
-
honeybadger almost 9 yearswould this work the same if I were to save my user's username and password as variables?
-
honeybadger almost 9 yearsThank you so so much :) i'm a total newbie at this as you can tell
-
Reut Sharabani almost 9 yearsWe were all new to programming once. You can accept the answer if it's valid.
-
Reut Sharabani almost 9 years@honeybadger how did you end up serializing your data (string? dictionary? list?)? I'd suggest you print the data or use a debugger. if there is a problem open another question after reading this: stackoverflow.com/help/mcve
-
honeybadger almost 9 yearsI used the code that you posted me to do it, but the only problem would be the quotation marks..
-
Reut Sharabani almost 9 years@honeybadger they are not a problem as long as you use the same mechanism (
json
/pickle
/your own). What you get fromserializer.load
is what you put inserializer.dump(something, f)
. For more information read this: en.wikipedia.org/wiki/Serialization . Did you try and debug your code using a debugger? Did you try and print the variables to see what values they hold? Share the attempts you made to understand the code. -
honeybadger almost 9 yearsI've tried debugging the program online but they basically just run the program till it stops at the error. I've been looking for solutions but none seem to work, I was trying to just strip the word from the text file but that doesn't seem to be working either. I've even tried adding the quotation marks inside the coding as text so that it might somehow be able to log in but that just caused a syntax error
-
Reut Sharabani almost 9 years@honeybadger Best advice I can give you to become way more productive and flatten your learning curve is to download and use PyCharm community edition which is free. Use it to write and debug your code: jetbrains.com/pycharm-educational/download . You can get a free copy of the professional edition for educational purposes if you're a student. Disclaimer: I use it a lot.
-
honeybadger almost 9 yearsI'll try. Thanks for the help, I really appreciate it