Currently, the program erases all entries that have been saved to a text file. This is done when the user calls on the clear_path
function by clicking on a listBox item then clicking the clear button.
from Tkinter import *
import os
root = Tk()
root.title("MyPath")
#root.iconbitmap(default='SpSoLogo.png.ico')
class MainGui:
def __init__(self, master):
self.mypathLabel = Label(master, text="Copy and Paste (Ctrl-V) your filepath here:")
self.mypathEntry = Entry(master, bg="black", fg="white", relief=SUNKEN)
self.mypathSaveButton = Button(master, text="Save Path", bg="black", fg="white", command=self.save_path)
self.mypathLoadButton = Button(master, text="Load Paths", bg="black", fg="white", command=self.load_paths)
self.mypathClearButton = Button(master, text="Clear Path", bg="black", fg="white", command=self.clear_path)
self.mypathList = Listbox(master, bg="black", fg="white")
self.mypathScroll = Scrollbar(master, orient=HORIZONTAL, bg="black")
self.mypathScroll.config(command=self.mypathList.xview)
self.mypathLabel.pack()
self.mypathEntry.pack()
self.mypathSaveButton.pack()
self.mypathLoadButton.pack()
self.mypathClearButton.pack(side=BOTTOM)
self.mypathList.pack()
self.mypathList.bind("<Double-Button-1>", self.open_path, self.clear_path)
self.mypathScroll.pack()
def load_paths(self):
with open("favorites.txt", "r") as f:
for line in f:
self.mypathList.insert(END, line)
def clear_path(self):
if self.mypathList.curselection():
getOut = self.mypathList.curselection()
self.mypathList.delete(getOut)
else:
self.mypathList.delete(0, 'end')
#####Everything Erases from the text file#####
badPath = [self.mypathList.curselection()]
f = open("favorites.txt", "r")
lines = f.readlines()
f.close()
f = open("favorites.txt", "w")
for line in lines:
if line == badPath:
f.delete(line)
f.close()
def save_path(self):
fp = self.mypathEntry.get()
scribe=open("favorites.txt", "a")
scribe.write(fp + '\n')
self.mypathEntry.delete(0, 'end')
self.mypathList.insert(1, fp)
def open_path(self, event):
list_item = self.mypathList.curselection()
fp = self.mypathList.get(list_item[0])
os.system('Explorer "%s"' % fp)
## print(fp)
## try:
## with open(fp, 'r') as result:
## print(result.read())
## except Exception as e:
## print(e)
MainGui(root)
root.mainloop()
Q.) Why does this happen and how do I change it to only delete the selected item?