我想将变量(包括其值)保存到文本文件中,以便下次打开程序时,任何更改都会自动保存到文本文件中 例如:
balance = total_savings - total_expenses
我如何将变量本身保存到文本文件中而不仅仅是其值? 本节适用于注册页面
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()
修改代码:
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)
登录代码:
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()
答案 0 :(得分:3)
您必须在编译时知道变量的名称。所以你需要做的就是:
with open('some_file.txt', 'w') as f:
f.write("balance %d" % balance)
使用dict
对象将名称映射到值可以更方便地进行管理。
您可能还想了解pickle
或json
模块,这些模块可以轻松序列化dict
等对象。
使用pickle
等序列化程序的方法是:
import pickle as serializer
balance = total_savings - total_expenses
with open('some_file.txt', 'w') as f:
serializer.dump( balance, f)
您可以在提供的代码中将pickle
更改为json
,以使用其他标准序列化程序并以json
格式存储对象。
在您的示例中,您尝试存储来自tkinter
的{{1}}小部件的文字。阅读它here。
您可能错过了使用StringVariable捕获输入的文本:
为变量创建Entry
:
StringVar
将StringVar变量注册到Entry小部件:
username = StringVar()
password = StringVar()
使用e1 = Entry (register, textvariable=username)
e2 = Entry (register, textvariable=password, show= "*")
在两个单独的文件中保存内容:
StringVar
如果你想在同一个文件中创建一个映射(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)
)并存储它:
dict
您使用序列化之前输入文字。将import json as serializer
with open('godhelpme.txt', 'w') as f:
serializer.dump(
{
"username": username.get(),
"password": password.get()
}, f
)
函数(稍后可以退出)注册到注册按钮。通过这种方式,用户点击它后会被称为 (这意味着内容已经存在)。方法如下:
save
之前发生的事情是在用户插入任何数据之前发生了保存到文件的过程。通过向按钮单击注册功能,您可以确保仅在按下按钮时才执行该功能。
我强烈建议您在调试环境中使用旧代码或使用一些打印来确定代码的工作方式。
答案 1 :(得分:0)
将变量存储在.txt
文件中通常不是一个好习惯,Python有一个非常好的库Pickle。但是,您仍然可以分析这两种方法并选择一种方法。
方法1:
使用.txt
文件:
with open("variable_file.txt", "w") as variable_file:
variable_file.write("a = 10")
在检索您可以使用的值时:
with open("variable_file.txt", "r") as variable_file:
for line in variable_file.readlines():
eval(line.strip())
方法2:
使用Pickle模块:
import pickle
a = 10
pickle.dump( a, open( "save.p", "wb" ) )
#Load the variable back from the pickle file.
a = pickle.load( open( "save.p", "rb" ) )