views:

127

answers:

5

I wrote the beautiful python example code below. Now how do I make it so when I exit then restart the program it remembers the last position of the scale?

import Tkinter

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')
root.mainloop()

Edit:

I tried the following code

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
+4  A: 

Write the scale value to a file and read it in on startup. Here's one way to do it (roughly),

CONFIG_FILE = '/path/to/config/file'

root.sclX = ...

try:
    with open(CONFIG_FILE, 'r') as f:
        root.sclX.set(int(f.read()))
except IOError:    # this is what happens if the file doesn't exist
    pass

...
root.mainloop()

# this needs to run when your program exits
with open(CONFIG_FILE, 'w') as f:
    f.write(str(root.sclX.get()))

Obviously you could make it more robust/intricate/complicated if, for instance, you want to save and restore additional values.

David Zaslavsky
I tried this way but get the following error: Traceback (most recent call last): File "<string>", line 244, in run_nodebug File "C:\Python26\pickleexample.py", line 30, in <module> with open(CONFIG_FILE, 'w') as f:IOError: [Errno 2] No such file or directory: 'Libraries\\Documents\\T.txt' How do I set up the file directory properly?
Anteater7171
That probably means that one of the parent directories of the file you're trying to create does not exist. Make sure that the parent directory of the file you specify in `CONFIG_FILE` exists. For instance, if you set `CONFIG_FILE='Libraries\Documents\T.txt'`, make sure that the directory `Libraries\Documents` (relative to the directory in which you run the Python script) exists. Although I would actually recommend using an absolute path (one that starts with a drive letter), e.g. `CONFIG_FILE = 'C:\SomeFolder\Libraries\Documents\T.txt'`. It's less confusing that way.
David Zaslavsky
+1  A: 

You can tell the program to write a file called for example "save.txt" with the parameter and then load it in further executions:

Is there any "save.txt"?

No: Write a new save file with the parameter. Yes: Read the parameter and pass it to the variable.

If the parameter is updated then rewrite it in the file.

I'm not expert on Python but, there should be some nice library to read and write files :)

aitorkun
+3  A: 

Just before the mainloop:

import cPickle
with open('myconfig.pk', 'wb') as f:
  cPickle.dump(f, root.config(), -1)
  cPickle.dump(f, root.sclX.config(), -1)

and, on subsequent runs (when the .pk file is already present), the corresponding cPickle.load calls to get it back and set it with ...config(**k) (also needs some trickery to confirm to cPickle that the pickled configuration is safe to reload, unfortunately).

Alex Martelli
A: 

A simple use of

help(cPickle.dump)

in the interpreter will immediately reveal that you should try changing Alex's code by swapping f and the object stored in the call:

cPickle.dump(root.config(), f, -1)
cPickle.dump(root.sclX.config(), f, -1)
Domingo Galdos
A: 

Hello,

For objet persistence there is a nice module in Python : shelve.

e.g. (The two scripts are in the same directory)

script 1

import shelve

settings = shelve.open('mySettings')
settings['vroot_directory'] = commands.getoutput("pwd")

script 2

import shelve 

settings = shelve.open('mySettings')
root_directory_script1 = settings['vroot_directory'])   

rgds,

ps. for a more complete example the answer of my post by g.d.d.c : http://stackoverflow.com/questions/3500957/importing-python-variables-of-a-program-after-its-execution

Bruno