views:

13

answers:

1

Every time this code is reopened the insertion cursor moves one line lower, how do I stop this?

import Tkinter,pickle
class Note(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.Main()
        self.load_data()
        self.protocol("WM_DELETE_WINDOW", self.save_data)
    def Main(self):
        self.s1 = Tkinter.Scrollbar(self)
        self.L1 = Tkinter.Text(self,borderwidth=0,font=('Arial', 10),width=25,
        height=15)
        self.s1.config(command=self.L1.yview,elementborderwidth=3)
        self.L1.config(yscrollcommand=self.s1.set)
        self.L1.grid(column=0,row=0,sticky='EW')
        self.s1.grid(column=1,row=0,sticky='NSEW')
        self.L1.focus_set()
    def save_data(self):
        data = {'saved_data': self.L1.get('1.0', 'end')}
        with file('testsave.data', 'wb') as f:
            pickle.dump(data, f)
        self.destroy()
    def load_data(self):
        try:
            with file('testsave.data', 'rb') as f:
                data = pickle.load(f)
                self.L1.insert("end", data['saved_data'])
        except IOError:
            pass
if __name__ == "__main__":
    app = Note(None)
    app.mainloop()
+1  A: 

The text widget is guaranteed to always have a trailing newline at the end of its contents. The proper way to get the data is to use the index "end-1c" so that you don't get that extra newline. If you use "end", each save and load cycle adds one blank lone.

Bryan Oakley
Perfect, thanks!
Anteater7171