Python 2.6 中的 cPickle 帮助
我在 python 中尝试了以下代码。这是我第一次尝试腌制。
import Tkinter
import cPickle
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
with open('myconfig.pk', 'wb') as f:
cPickle.dump(f, root.config(), -1)
cPickle.dump(f, root.sclX.config(), -1)
root.mainloop()
但出现以下错误:
Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Python26\pickleexample.py", line 17, in <module>
cPickle.dump(f, root.config(), -1)
TypeError: argument must have 'write' attribute
我做错了什么?
编辑:
我尝试了以下代码,它有效!现在我该如何做到这一点,以便当程序重新启动时,秤处于与上次关闭程序时相同的位置?
import Tkinter
import cPickle
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
with open('myconfig.pk', 'wb') as f:
cPickle.dump(root.config(), f, -1);
cPickle.dump(root.sclX.config(), f, -1);
root.mainloop()
I tried the following code I python. This is my first attempt at pickling.
import Tkinter
import cPickle
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
with open('myconfig.pk', 'wb') as f:
cPickle.dump(f, root.config(), -1)
cPickle.dump(f, root.sclX.config(), -1)
root.mainloop()
But get the following error:
Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Python26\pickleexample.py", line 17, in <module>
cPickle.dump(f, root.config(), -1)
TypeError: argument must have 'write' attribute
What am I doing wrong?
EDIT:
I tried the following code, and it works! Now how do I make it so when the program is restarted the scale is in the same position it was when the program was last closed?
import Tkinter
import cPickle
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
with open('myconfig.pk', 'wb') as f:
cPickle.dump(root.config(), f, -1);
cPickle.dump(root.sclX.config(), f, -1);
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试切换参数的顺序:
根据 文档,该文件应该是第二个参数,要腌制的对象应该是第一个。
Try switching the order of the arguments:
According to the documentation, the file should be the second argument, and the object to be pickled should be the first.
我认为您的参数顺序错误。请参阅此处的文档。尝试以下:
I think you have the parameters in the wrong order. See the docs here. Try below: