Home > Back-end >  How can I delete an option from a listbox and having that reflect in a text file?
How can I delete an option from a listbox and having that reflect in a text file?

Time:10-08

I have a listbox of saved values that are also saved into a .txt file. I can delete options in the listbox just fine, but I am having a hard time finding out how to reflect the deleted values in its .txt file.

My values are set up to be a dictionary and my options are:

Option 1: I could read the text file as a dictionary and remove the key that corresponds to the selected listbox.

Option 2: I could, after deleting the selected listbox, rewrite the .txt to include every remaining listbox.

edit: To clarify, I have a listbox. This listbox is made from contents in a .txt. I have a button to delete an option in the listbox. I need the button to delete the value in the .txt too, but I am having troubles with it.

Any help would be appreciated.

CodePudding user response:

Thanks @acw1668

for index in listbox.curselection():           #Set the current selection to be deleted
    listbox.delete(index)

with open("saved.txt","w") as f:               #Takes the new listbox, without 
     for index in listbox.get(0, END):         #deleted item, and rewrote   
        f.write(index)                            #the saved.txt         
  • Related