My code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 14:47:36 2017
@author: Jose Chong
"""
import json
import tkinter
import os
def checkExistenceOfSaveFile():
if not os.path.isfile(filepath):
os.makedirs(filepath, 777)
checkExistenceOfSaveFile()
filepath = os.path.expanduser(r'~\Documents\joseDzirehChongToDoList\toDoListSaveFile.json')
master = tkinter.Tk()
master.title("To-Do List")
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 = []
if os.path.getsize(filepath) == 0:
with open (filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
with open(filepath) as infile:
checkboxList = json.load(infile)
def destroyCheckbox(checkbox, row):
row.destroy()
del checkboxList [checkboxList.index(str(checkbox))]
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
for savedCheckbox in checkboxList:
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text=savedCheckbox)
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text="x", bg="red", fg="white",
activebackground="white", activeforeground="red",
command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r))
deleteItem.pack(side=tkinter.RIGHT)
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
def drawCheckbox():
newCheckboxInput = entry.get()
def destroyCheckbox():
checkboxRow.destroy()
del checkboxList[checkboxList.index(newCheckboxInput)]
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
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=lambda c=checkbox1, r=checkboxRow: destroyCheckbox(), bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
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()
In this instance of running the code, joseDzirehChongToDoList and the save file were not yet created, so they were created by the program.
Error here:
runfile('C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py', wdir='C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile')
Traceback (most recent call last):
File "<ipython-input-84-f554d849ad97>", line 1, in <module>
runfile('C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py', wdir='C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile')
File "C:\Users\Josalina\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Josalina\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Josalina/Desktop/Codi'C:\\Users\\Josalina\\Documents\\joseDzirehChongToDoList\\toDoListSaveFile.json'ng/Language - Python/to-do-list-to-compile/toDoList.py", line 36, in <module>
with open (filepath, 'w') as outfile:
PermissionError: [Errno 13] Permission denied:
Line 36 is the offender here, which is this:
with open (filepath, 'w') as outfile:
The context of this code is this:
if os.path.getsize(filepath) == 0:
with open (filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
It checks if toDoListSaveFile.json is empty (filepath
is the filepath of the file), and if it is an empty list is added to it. This was done because I have json.load()
later and you can't load an empty file.
As you can see though, I made the file using 777 permissions as seen in checkExistenceOfSaveFile()
. Doesn't that mean everyone can read and write to the file? Or do I have to change the permissions so my program can write stuff to it.