You need many changes and fixes to make your code work as intended:
import Tkinter
import cPickle
root = Tkinter.Tk()
place = 0
root.place = Tkinter.IntVar()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1,
variable=root.place)
root.sclX.pack(ipadx=75)
try:
with open('myconfig.pk', 'rb') as f:
place = cPickle.load(f)
except IOError:
pass
else:
root.place.set(place)
def tracer(*a):
global place
place = root.place.get()
root.place.trace('r', tracer)
root.resizable(False, False)
root.title('Scale')
root.mainloop()
with open('myconfig.pk', 'wb') as f:
cPickle.dump(place, f, -1);
Let's look at the changes from the top. I've introduced a Tkinter variable root.place
so that the position of the scale can be tracked at all times (via later function tracer
) in global variable place
(it would be more elegant to use OOP and avoid global variables, but I'm trying to keep things simple for you;-).
Then, the try/except/else statement changes the setting of place
iff the .pk
file is readable. You were never trying to read back what you had saved.
Last but not least, I've moved the save operation to after mainloop exits, and simplified it (you don't need all of the config dictionaries -- which you can't access at that point anyway -- just the place
global). You were saving before mainloop started, therefore "saving" the initial values (not those changed by the main loop execution).
The tracer
function and .trace
method calls ensure that global variable place
always records the scale's latest position -- so that it can be recovered, and saved, after mainloop has exited (after mainloop exits, all Tkinter objects, both GUI ones and variables, become inaccessible).