首页 > 解决方案 > 将 tkinter Filedialog 与 json.dump 结合使用

问题描述

我花了一个小时的大部分时间来研究这个,我敢肯定,这是一个非常简单的问题。
我要做的就是从我的程序中使用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(),这次好像看不到我的错误

谢谢!

标签: pythonjsontkinter

解决方案


所以我在 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()

推荐阅读