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

1
  • 1
    If you inspect the saved text file, you will find that you have saved the content in one line due to the line f.writelines(str(list1)). Commented Apr 23, 2020 at 4:41

1 Answer 1

3

Two solutions:

  1. Change the way you put the file data in the Listbox.
  2. Change the way you save them to the file.

For the first solution,because you save a string with list format.While you read this saved file,the string will be:

enter image description here

You need to change the string to list.An easy way:

import ast

....

read = open("file.txt","r")
data_list = ast.literal_eval(read.read())
read.close()

Or use some string-processing operation.That's a little complex.

For the second solution,just change the way you save it.add a \n in each line.(Because you use .readlines() to read the file.)

def save():
    with open("file.txt","w") as f:
        for i in listbox.get(0,END):
            f.write(i+"\n")
Sign up to request clarification or add additional context in comments.

2 Comments

It worked perfectly!! Thank you. But I have another problem coming - which is - now the data of Mike and Bob are saved but when I open the application and select "Mike" or "Bob", and try to delete that entry, it is showing me an error. Like : Listbox.delete(index) TypeError: delete() missing 1 required positional argument: 'first' . How do I fix this? Thank you so much for helping.
@TejasDhanani ...You used Listbox.delete(index) which should be listbox.delete(index) because your Listbox widget is variable listbox.Listbox is a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.