问题描述
我正在使用tkinter,每当我按下按钮时,我都想在文件中写入一个字符串。 但是,由于可以从tkinter条目小部件更改字符串,因此在文件中仅写入最后一个字符串。 如何在不同行中将tkinter中的每个字符串写入并保存到文件中?
with open("Accountdata.txt","r+") as f:
f.write(str(usernames))
f.write("\n")
print(f.read())
f.close()
解决方法
with open(filename,'a+') as f: # append (plus) mode
f.write(str(usernames) + '\n')
f.seek(0)
print(f.read())