2010-07-02 61 views
4

我試過下面的代碼我python。這是我第一次嘗試酸洗。幫助Python 2.6中的cPickle

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() 
+0

也許將問題標記爲已接受/將EDIT移至單獨的問題中?雖然我懷疑你再有這個問題了...... – 2015-11-21 01:28:04

回答

1

我想你有錯誤的順序參數。請參閱文檔here。嘗試下面:

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

嘗試切換的參數的順序:

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

根據the documentation,該文件應是第二個參數,並且對象被酸洗應該是第一。