我创建了一些我希望保存在JSON或XML文件中的对象(使用tkinter
小部件),以便我可以在启动后恢复它们。将对象保存到Python中的JSON或XML文件中
from Tkinter import *
class Texte:
def __init__(self, ax, ay, txt):
self.entry = Entry(root,bd=0,font=("Purisa",int(15)))
self.entry.insert(0, txt)
self.x = ax
self.y = ay
self.entry.place(x=self.x,y=self.y)
root = Tk()
a = Texte(10, 20, 'blah')
b = Texte(20, 70, 'blah2')
# here the user will modify the entries' x, y, txt, etc.
L = [a,b]
# here save the list L (containing the Texte objects) into a JSON file or XML so that I can recover them after restart
root.mainloop()
如何保存和恢复使用JSON或XML这些对象呢?
(我有点与http://docs.python.org/2/library/json.html现在丢失。)
是这么简单吗? (看来我需要一个'write()'作为我的对象,你对我的示例widget有什么用处?) – Basj
不,你不需要写它。 'json.dump'会自动将你需要的内容导出到指定的文件中。 – aIKid
我试图将你的代码粘贴到我的末尾,我得到: 'AttributeError:'str'object has no attribute'write'' – Basj