views:

90

answers:

3

I have tried unsuccessfully several times to get programs to remember settings after they've been destroyed. A large reason for that is because I don't have an example code to work off of. Below I have a simple program I wrote. I'd like it so that it both remembers the position of the scale, and the contents of the text widget upon restarting the program. I hate having to ask someone to write code for me, but I'm honestly stuck.

I'm using Python 2.6.5 on Windows 7, BTW.

Code:

import Tkinter


class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.sclX = Tkinter.Scale(self, from_=0, to=100, orient='horizontal',resolution=1,command=self.A)
        self.sclX.pack(ipadx=75)


        self.labelVar = Tkinter.StringVar()
        self.label = Tkinter.Label(self,textvariable=self.labelVar)
        self.label.pack(ipadx=75)


        self.frame = Tkinter.Frame(self,relief='ridge',borderwidth=4)
        self.frame.pack()
        self.LVariable = Tkinter.StringVar()
        self.s = Tkinter.Scrollbar(self.frame)
        self.L = Tkinter.Text(self.frame,borderwidth=0,font=('Arial', 10),width=30, height=15)
        self.s.config(command=self.L.yview,elementborderwidth=1)
        self.L.grid(column=0,row=0,sticky='EW')
        self.s.grid(column=1,row=0,sticky='NSEW')


    def A(self, event):
        self.labelVar.set(100 - self.sclX.get())


if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.mainloop()
+3  A: 

It's not a matter of asking people to write code for you but knowing what to look for, you could write your own code after that!

I think remembering settings like you describe is commonly done in two ways:

  1. Config file
  2. Registry entries

You can then read in the stored value from either the config file/registry whenever your program loads, and adjust parameters to match.

So now you go look up how you read/write files/registry entries and you are set!

filip-fku
+2  A: 

Depending on how you prefer to store settings you can also look into things like Shelve and Pickle/cPickle. I personally prefer Shelve because I tend to use dictionaries as settings containers and Shelve lets me store those as is. Full documentation available here: http://docs.python.org/library/shelve.html

g.d.d.c
+1  A: 

Your previous question where you had difficulty saving state using cPickle was a good start.

I have added a couple of methods to your code and it will now save and load the data in the Scale and Text widgets using the pickle module. I've never used Shelve - that sounds like it would be easier based on what g.d.d.c says in his(?) answer.

I store the widget values in a dictionary, then pickle the dictionary.

import Tkinter
import pickle

class simpleapp_tk(Tkinter.Tk):
    def __init__(self, parent=None):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()
        self.load_data()
        self.protocol("WM_DELETE_WINDOW", self.save_data)

    def initialize(self):
        self.sclX = Tkinter.Scale(self, from_=0, to=100, orient='horizontal',
            resolution=1,command=self.update_label)
        self.sclX.pack(ipadx=75)

        self.labelVar = Tkinter.StringVar()
        self.label = Tkinter.Label(self,textvariable=self.labelVar)
        self.label.pack(ipadx=75)

        self.frame = Tkinter.Frame(self,relief='ridge',borderwidth=4)
        self.frame.pack()
        #self.LVariable = Tkinter.StringVar()
        self.s = Tkinter.Scrollbar(self.frame)
        self.L = Tkinter.Text(self.frame, borderwidth=0, font=('Arial', 10),
            width=30, height=15)
        self.s.config(command=self.L.yview, elementborderwidth=1)
        self.L.grid(column=0, row=0, sticky='EW')
        self.s.grid(column=1, row=0, sticky='NSEW')

    def update_label(self, event):
        self.labelVar.set(100 - self.sclX.get())

    def save_data(self):
        data = {'scale': self.sclX.get(), 'text': self.L.get('1.0', 'end')}
        with file('config.data', 'wb') as f:
            pickle.dump(data, f)
        self.destroy()

    def load_data(self):
        try:
            with file('config.data', 'rb') as f:
                data = pickle.load(f)
                self.sclX.set(data['scale'])
                self.L.insert("end", data['text'])
        except IOError:
            # no config file exists
            pass

if __name__ == "__main__":
    app = simpleapp_tk()
    app.mainloop()
PreludeAndFugue
Thanks that was exactly what I was looking for. Now I have an adequate example code to work off of.
Anteater7171