将tkinter Filedialog与json.dump结合使用

时间:2018-06-19 09:59:06

标签: python json tkinter

我花了大约一小时的时间来研究这个问题,我确信这个问题非常简单。
我要做的就是使用json.dump()从我的程序中导出字典。 我想使用tk.filedialog.asksaveas ...

选择路径
def exportCSV(container):
exCsDi = filedialog.asksaveasfilename()
if not exCsDi:
    return
with open (exCsDi.name) as file:
    json.dump(container.Dict, open(file ,'w'))
file.close()
container.leavemini()

但这不起作用。 我得到AttributeError: 'str' object has no attribute 'name'

我以前成功使用过json.dump(),这次我似乎无法看到我的错误

谢谢!

1 个答案:

答案 0 :(得分:0)

因此,在Michael K.帮助我发现另一个问题之后,我找到了解决方案:

def exportCSV(container):
exCsDi = filedialog.asksaveasfilename()
if not exCsDi:
    return
with open (exCsDi, 'w') as file:
    json.dump(container.Dict, file)
container.destroy()