【问题标题】:Help with cPickle in Python 2.6Python 2.6 中的 cPickle 帮助
【发布时间】:2011-03-11 06:42:47
【问题描述】:

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

【问题讨论】:

  • 也许将问题标记为已接受/将编辑移动到单独的问题中?虽然我怀疑你还有这个问题......

标签: python tkinter python-2.6 pickle


【解决方案1】:

尝试切换参数的顺序:

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

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

【讨论】:

    【解决方案2】:

    我认为您的参数顺序错误。请参阅文档here。试试下面:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2011-06-10
      • 2021-11-07
      • 2011-06-09
      相关资源
      最近更新 更多