views:

27

answers:

2

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()
A: 

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);
Taylor Leese
+2  A: 

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.

David Zaslavsky