我的代码。 filepath
的内容是我认为的重要内容。
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 14:47:36 2017
@author: Jose Chong
"""
import json
import tkinter
import os
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 = []
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=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r), 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()
使用print(filepath)我可以看到文件路径是有效的,&#34; C:\ Users \ Josalina \ Documents \ joseDzirehChongToDoList \ toDoListSaveFile.json&#34;打印出来。但是,我收到此错误(如果可能需要,请转到错误的末尾,其余部分仅用于上下文)
Traceback (most recent call last):
File "<ipython-input-33-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/Coding/Language - Python/to-do-list-to-compile/toDoList.py", line 30, in <module>
checkboxList = json.load(infile)
File "C:\Users\Josalina\Anaconda3\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Josalina\Anaconda3\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\Josalina\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Josalina\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
我甚至没有357行,我只有107行代码。我该如何解决?我们的想法是,它应该将列表中的项添加到JSON文件中,基本上是this,但JSON文件位于不同的位置。
答案 0 :(得分:1)
这是一个空文件......程序拒绝运行,因此它是一个空文件
你不能json.load
一个空文件。这就是程序拒绝运行的原因。
你的代码在这里......
checkboxList = []
with open(filepath) as infile:
checkboxList = json.load(infile)
您只需将[]
放入文件即可。它不能是空的。
该列表应该在程序第一次运行时添加到文件中
然后你需要这样的东西来添加“到文件”,而不是“从文件”中读取
with open(filepath, 'w') as fp:
json.dump(checkboxList, fp)