from tkinter import*
import webbrowser
def add():
name = entry1.get()
id = entry2.get()
listbox.insert(END, name+ " : " +id)
def delete():
select = listbox.curselection()
index = select[0]
Listbox.delete(index)
def save():
list1 = list(listbox.get(0,END))
f = open("file.txt","w")
f.writelines(str(list1))
f.close()
read = open("file.txt","r")
data_list = read.readlines()
read.close()
data_list = [data.rstrip() for data in data_list]
win = Tk()
win.title("Class")
frame1=Frame(win)
frame2=Frame(win)
frame1.pack()
frame2.pack()
label1 = Label(frame1,text="Name : ")
label1.grid(row=0,column=0)
label2 = Label(frame1,text="Id : ")
label2.grid(row=1,column=0)
name = StringVar()
entry1 = Entry(frame1,textvariable=name)
entry1.grid(row=0,column=1)
id = StringVar()
entry2 = Entry(frame1,textvariable=id)
entry2.grid(row=1,column=1)
scrollbar = Scrollbar(frame2,orient=VERTICAL)
listbox = Listbox(frame2,selectmode=EXTENDED,yscrollcommand=scrollbar.set,width=60)
listbox.pack()
scrollbar.config(command=listbox)
for item in data_list:
listbox.insert(END,item)
button1 = Button(frame2,text="Add",command=add)
button1.pack()
button2 = Button(frame2,text="Delete",command=delete)
button2.pack()
button3 = Button(frame2,text="Save to File",command=save)
button3.pack()
win.mainloop()
[Please open and see the below uploaded screenshots for better understanding of the question]
While performing the application first time
Screenshot of opening application for 1st time
1. Adding "Mike : 11" and "Bob : 22". Here both are in separate lines .
2. Saving the file and closing the application.
While restarting the application
Screenshot of opening application for 2nd time
1. Why does the data of Mike and Bob came into same like and how can I make this thing load in separate lines
(exactly like:
Mike : 11
Bob : 22
)

f.writelines(str(list1)).