Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update/append values in text file using Tkinter

I wrote a code that takes user input and saves it as a text file.

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()

Right now, when i want to add new data to my text file the values get modified.

Is there any way i can update my text file by appending the new data instead of overwriting it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want an output in txt file like this:

Ques1

Choices1

correct_ans1

marks1

time1

Ques2

Choices2

correct_ans2

marks2

time2

>Solution :

Of course there is. Use file = open("user.txt","a"), as the documentation says. And note that ALL of those things will be strings; there’s no need to call str. And you can do that all in one call:

print('\n'.join([question_info, choices_info, correct_answer_info, marks_info, time_info]), file=file)

I use print because it also adds a trailing newline, which you were not doing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading