0

I tried adding a text file to store all the data by building a function that opens and writes data to the file, the code seems to run but no data is getting added to the text file.

Here is the tkinter code.

name = StringVar()
email = StringVar()
usn = StringVar()

namelabel = ttk.Label(frame_content, text='Name',font=('Poppins', 10))
namelabel.grid(row=0, column=0, sticky='sw', padx=(2,10))
entry_name = ttk.Entry(frame_content, width=30, font=('Poppins', 10), textvariable=name)
entry_name.grid(row=1, column=0)

emaillabel = ttk.Label(frame_content, text='Email',font=('Poppins', 10))
emaillabel.grid(row=0, column=1, sticky='sw', padx=10)
entry_email = ttk.Entry(frame_content, width=30, font=('Poppins', 10), textvariable=email)
entry_email.grid(row=1, column=1, padx=10)

usnlabel = ttk.Label(frame_content, text='USN',font=('Poppins', 10))
usnlabel.grid(row=0, column=2, sticky='sw')
entry_usn = ttk.Entry(frame_content, width=30, font=('Poppins', 10), textvariable=usn)
entry_usn.grid(row=1, column=2)

feedbacklabel = ttk.Label(frame_content, text='Feedback', font=('Poppins', 10))
feedbacklabel.grid(row=2, column=0, sticky='sw', pady=5)
textfeedback = Text(frame_content, width=94, height=15, font=('Poppins', 10))
textfeedback.grid(row=3, column=0, columnspan=3)

def submit():
    print('Name:{}'.format(name.get()))
    print('USN:{}'.format(usn.get()))
    print('Email:{}'.format(email.get()))
    print('Feedback:{}'.format(textfeedback.get(1.0, END)))
    messagebox.showinfo(title='Submit', message='Thank you for your Feedback')
    entry_name.delete(0, END)
    entry_email.delete(0, END)
    entry_usn.delete(0, END)
    textfeedback.delete(1.0, END)

def open_file():
    name_info = name.get()
    email_info = email.get()
    usn_info = usn.get()
    feed_info = textfeedback.get(1.0, END)

    data_file = open("data.txt", 'w')
    data_file.write(name_info)
    data_file.write(email_info)
    data_file.write(usn_info)
    data_file.write(feed_info)
    data_file.close()


submitbutton = ttk.Button(root, width=25, text='Submit', command=lambda: [submit(), open_file()])
mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
hidanbog
  • 1
  • 1
  • Where and how does it fail? Does this already answer your question: [how do i append to a file](https://stackoverflow.com/questions/4706499/how-do-i-append-to-a-file)? – Friedrich Jan 30 '23 at 08:13
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 30 '23 at 16:16

1 Answers1

0

EDIT

Your mistake can be at

submitbutton = ttk.Button(root, width=25, text='Submit', command=lambda: [submit(), open_file()])

Call open_file() function first, then call submit() function, like:

submitbutton = ttk.Button(root, width=25, text='Submit', command=lambda: [open_file(), submit()])
  • @Friedrich `CSV` _`(Comma Seperated Values)`_ is used to store data like same you store in a database. In your case its best to use `csv` as you can do data manipulation with that data in future, if needed. – Adarsh Gupta Jan 30 '23 at 07:58
  • @Friedrich from my side it is saving in `data.txt` – Adarsh Gupta Jan 30 '23 at 08:12
  • Bottom line is: if you know the answer, please post it and thank you very much. If you don't know and are just guessing or want to inspire random improvements, either write a comment or don't write anything at all. There's much noise here already and "might be" and "could be" don't help anybody. Your contributions can help to make SO even better. – Friedrich Jan 30 '23 at 08:27