Python 2.6 中的 cPickle 帮助

发布于 2024-09-08 05:09:28 字数 1184 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-09-15 05:09:28

尝试切换参数的顺序:

cPickle.dump(root.config(), f, -1)
cPickle.dump(root.sclX.config(), f, -1)

根据 文档,该文件应该是第二个参数,要腌制的对象应该是第一个。

Try switching the order of the arguments:

cPickle.dump(root.config(), f, -1)
cPickle.dump(root.sclX.config(), f, -1)

According to the documentation, the file should be the second argument, and the object to be pickled should be the first.

请叫√我孤独 2024-09-15 05:09:28

我认为您的参数顺序错误。请参阅此处的文档。尝试以下:

cPickle.dump(root.config(), f, -1);
cPickle.dump(root.sclX.config(), f, -1);

I think you have the parameters in the wrong order. See the docs here. Try below:

cPickle.dump(root.config(), f, -1);
cPickle.dump(root.sclX.config(), f, -1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文