Update/append 使用 Tkinter 的文本文件中的值
Update/append values in text file using Tkinter
我编写了一段代码,它接受用户输入并将其保存为文本文件。
from tkinter import *
def save_info():
question_info = question.get()
choices_info = choices.get()
correct_answer_info = correct_answer.get()
marks_info = marks.get()
time_info = time.get()
#print('all values')
file = open("user.txt","w")
file.write(question_info)
file.write("\n")
file.write(choices_info)
file.write("\n")
file.write(correct_answer_info)
file.write("\n")
file.write(str(marks_info))
file.write("\n")
file.write(str(time_info))
file.close()
app = Tk()
app.geometry("600x600")
app.title("Assesment Question")
heading = Label(text="Enter the Asked Information",fg="black",bg="yellow",width="500",height="3",font="10")
heading.pack()
question_text = Label(text="Question")
choices_text = Label(text="Choices separated by #")
correct_answer_text = Label(text="Correct Answer")
marks_text = Label(text='Marks')
time_text = Label(text="Time in seconds")
question_text.place(x=15,y=70)
choices_text.place(x=15,y=140)
correct_answer_text.place(x=15,y=210)
marks_text.place(x=15,y=280)
time_text.place(x=15,y=350)
question = StringVar()
choices = StringVar()
correct_answer = StringVar()
marks = IntVar()
time = IntVar()
question_entry = Entry(textvariable=question,width="30")
choices_entry = Entry(textvariable=choices,width="30")
correct_answer_entry = Entry(textvariable=correct_answer,width="30")
marks_entry = Entry(textvariable=marks,width="30")
time_entry = Entry(textvariable=time,width="30")
question_entry.place(x=15,y=100)
choices_entry.place(x=15,y=180)
correct_answer_entry.place(x=15,y=240)
marks_entry.place(x=15,y=300)
time_entry.place(x=15,y=380)
button = Button(app,text="Submit Data",command=save_info,width="30",height="2",bg="grey")
button.place(x=15,y=420)
mainloop()
现在,当我想将新数据添加到我的文本文件时,值会被修改。
有什么方法可以通过附加新数据而不是覆盖它来更新我的文本文件?
我想要这样的 txt 文件输出:
问题 1
选择 1
correct_ans1
标记 1
time1
问题2
选择2
correct_ans2
标记2
time2
当然有。如文档所述,使用 file = open("user.txt","a")
。
print('\n'.join([question_info, choices_info, correct_answer_info, str(marks_info), (time_info)]), file=file)
我使用 print
因为它还添加了一个尾随换行符,而您没有这样做。
或者:
print( f"{question_info}\n{choices_info}\n{correct_answer_info}\n{marks_info}\n{time_info}", file=file)
这个很简单
只需使用
file = open("user.txt","a")
而不是
file = open("user.txt","w")
这个 'a' 的作用是以附加模式打开文件,这意味着它会将新数据与以前的数据一起添加到您的文件中。
如果你使用 'w' 写入模式,它会添加新数据,但会删除以前的数据。
所以你最终只会得到你刚才输入的数据。
我编写了一段代码,它接受用户输入并将其保存为文本文件。
from tkinter import *
def save_info():
question_info = question.get()
choices_info = choices.get()
correct_answer_info = correct_answer.get()
marks_info = marks.get()
time_info = time.get()
#print('all values')
file = open("user.txt","w")
file.write(question_info)
file.write("\n")
file.write(choices_info)
file.write("\n")
file.write(correct_answer_info)
file.write("\n")
file.write(str(marks_info))
file.write("\n")
file.write(str(time_info))
file.close()
app = Tk()
app.geometry("600x600")
app.title("Assesment Question")
heading = Label(text="Enter the Asked Information",fg="black",bg="yellow",width="500",height="3",font="10")
heading.pack()
question_text = Label(text="Question")
choices_text = Label(text="Choices separated by #")
correct_answer_text = Label(text="Correct Answer")
marks_text = Label(text='Marks')
time_text = Label(text="Time in seconds")
question_text.place(x=15,y=70)
choices_text.place(x=15,y=140)
correct_answer_text.place(x=15,y=210)
marks_text.place(x=15,y=280)
time_text.place(x=15,y=350)
question = StringVar()
choices = StringVar()
correct_answer = StringVar()
marks = IntVar()
time = IntVar()
question_entry = Entry(textvariable=question,width="30")
choices_entry = Entry(textvariable=choices,width="30")
correct_answer_entry = Entry(textvariable=correct_answer,width="30")
marks_entry = Entry(textvariable=marks,width="30")
time_entry = Entry(textvariable=time,width="30")
question_entry.place(x=15,y=100)
choices_entry.place(x=15,y=180)
correct_answer_entry.place(x=15,y=240)
marks_entry.place(x=15,y=300)
time_entry.place(x=15,y=380)
button = Button(app,text="Submit Data",command=save_info,width="30",height="2",bg="grey")
button.place(x=15,y=420)
mainloop()
现在,当我想将新数据添加到我的文本文件时,值会被修改。
有什么方法可以通过附加新数据而不是覆盖它来更新我的文本文件?
我想要这样的 txt 文件输出:
问题 1
选择 1
correct_ans1
标记 1
time1
问题2
选择2
correct_ans2
标记2
time2
当然有。如文档所述,使用 file = open("user.txt","a")
。
print('\n'.join([question_info, choices_info, correct_answer_info, str(marks_info), (time_info)]), file=file)
我使用 print
因为它还添加了一个尾随换行符,而您没有这样做。
或者:
print( f"{question_info}\n{choices_info}\n{correct_answer_info}\n{marks_info}\n{time_info}", file=file)
这个很简单 只需使用
file = open("user.txt","a")
而不是
file = open("user.txt","w")
这个 'a' 的作用是以附加模式打开文件,这意味着它会将新数据与以前的数据一起添加到您的文件中。 如果你使用 'w' 写入模式,它会添加新数据,但会删除以前的数据。 所以你最终只会得到你刚才输入的数据。