如何使用 tkinter 按钮写入 python 中的文件?

how to write to a file in python using a tkinter button?

我正在使用 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())