I have a username-password login system and would like to add a delete user function. The usernames are stored on the even lines of a text file (inc zero) and the passwords on odd numbered lines. Is there a way of adding only the even numbered lines to a listbox to be selected via curselection to be deleted as showing the passwords would be against the point. I currently have all of the contents of the text file showing in the listbox however would like only the usernames to be shown. To delete the lines I have copied the contents of the listbox to a new text file after removing the chosen line from the listbox. Would there also be a way to delete the corresponding password after the username to be deleted has been selected ?
def DelUser():
global rootDU
global listbox
rootDU = Tk()
rootDU.title('Delete user')
users = open(creds, 'r')
mylist = users.readlines()
users.close()
listbox = Listbox(rootDU, width=50, height=6)
listbox.grid(row=0, column=0)
yscroll = Scrollbar(command=listbox.yview, orient=VERTICAL)
yscroll.grid(row=0, column=1, sticky=N + S)
listbox.configure(yscrollcommand=yscroll.set)
enter1 = Label(rootDU, text='Click on the user to delete', width=50)
enter1.grid(row=1, column=0)
for item in mylist:
listbox.insert(END, item)
delButton = Button(rootDU, text='Delete', command=RemoveUser)
delButton.grid(columnspan=2, sticky=W)
delButton.grid(row=2, column=0)
def RemoveUser():
global listbox
try:
index = listbox.curselection()[0]
listbox.delete(index)
os.remove(creds)
newfile = 'tempfile.txt'
with open(newfile, 'w') as f:
f.write(''.join(listbox.get(0, END)))
f.close()
except IndexError:
pass