My code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 14:47:36 2017
@author: Jose Chong
"""
import json
import tkinter
master = tkinter.Tk()
master.title("To-Do List (with saving!)")
master.geometry("300x300")
masterFrame = tkinter.Frame(master)
masterFrame.pack(fill=tkinter.X)
checkboxArea = tkinter.Frame(masterFrame, height=26)
checkboxArea.pack(fill=tkinter.X)
inputStuff = tkinter.Frame(masterFrame)
checkboxList = []
with open('toDoListSaveFile.json') as infile:
checkboxList = json.load(infile)
for savedCheckbox in checkboxList:
n = savedCheckbox
def destroyCheckbox():
checkboxRow.destroy()
del checkboxList[checkboxList.index(str(savedCheckbox))]
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = n)
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", command=destroyCheckbox, bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
def drawCheckbox():
newCheckboxInput = entry.get()
def destroyCheckbox():
checkboxRow.destroy()
del checkboxList[checkboxList.index(newCheckboxInput)]
checkboxList.append(newCheckboxInput)
entry.delete(0,tkinter.END)
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1])
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", command=destroyCheckbox, bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
def createInputStuff():
paddingFrame = tkinter.Frame(inputStuff, height=5)
paddingFrame.pack(fill=tkinter.X)
buttonDone.pack()
inputStuff.pack()
buttonAdd.pack_forget()
master.bind('<Return>', lambda event: drawCheckbox())
def removeInputStuff():
inputStuff.pack_forget()
buttonAdd.pack()
buttonDone.pack_forget()
master.unbind('<Return>')
buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff)
buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff)
buttonAdd.pack()
topInput = tkinter.Frame(inputStuff)
bottomInput = tkinter.Frame(inputStuff)
topInput.pack()
bottomInput.pack()
prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?")
prompt.pack()
entry = tkinter.Entry(bottomInput, bd=3)
entry.pack(side=tkinter.LEFT)
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox)
buttonConfirm.pack(side=tkinter.LEFT)
master.mainloop()
with open("toDoListSaveFile.json", 'w') as outfile:
json.dump(checkboxList, outfile)
The relevant bit of my code is the for savedCheckbox in checkboxList:
bit (I think), so it would be a good idea to read through there, that's probably where the issue is.
The issue is to do with the loaded checkboxes from the save file, or rather their delete buttons. New checkboxes have delete buttons that work just fine, but the ones that have been loaded from the save file using for savedCheckbox in checkboxList:
have delete buttons that malfunction. What I mean by that is:
They all delete the last loaded item (from the save file). So if my save file was a list of four checkboxes ["f", "a", "c", "e"], every loaded delete button would delete "e". If you click the delete button next to "c", too bad, you're deleting "e". And if I try to delete anything after "e" is gone, it gives me the error "ValueError: 'e' is not in list". They also don't destroy checkboxRow if the last loaded checkbox has already been deleted, I'm not sure if that's a side effect of the checkbox not being deleted or if I messed that up too.
How do I make my delete buttons work properly and delete the checkboxes properly? Proper deletion is:
Checkbox's data removed from the JSON file
The entire checkboxRow the checkbox is in should be removed too, taking the checkbox, text next to the checkbox, and delete button with it.
For what "proper" deletion looks like take a look at drawCheckbox()
, that's where the working deletion boxes are made.