Search code examples
pythontkintertkinter-entry

Store entry values from previous Tkinter session in a loop


I'm trying to create a Tkinter GUI that saves all entry box values into a text file, then reads back data from this text file and display them by default when the next session is started. Let's say I have a text file with the following data:

one
two
three

Using the following code:

#!usr/bin/env python
from Tkinter import *

class Tracker(Tk):
    def __init__(self, var1, var2, var3):
        Tk.__init__(self)

        # Create label
        app_label = Label(self, text="Enter value")
        app_label.pack()

        self.entry1 = StringVar()
        self.entry1.set(var1)
        ent1 = Entry(self,textvariable=self.entry1)
        ent1.pack()
        self.entry2 = StringVar()
        self.entry2.set(var2)
        ent2 = Entry(self,textvariable=self.entry2)
        ent2.pack()
        self.entry3 = StringVar()
        self.entry3.set(var3)
        ent3 = Entry(self,textvariable=self.entry3)
        ent3.pack()

        # Track 'delete window' event
        self.protocol("WM_DELETE_WINDOW", self.handler)

    def handler(self):
        f = open("backup.txt", "w")
        f.write(self.entry1.get()+'\n'+self.entry2.get()+'\n'+self.entry3.get())
        f.close()
        self.destroy()

if __name__ == "__main__":

    t = open("backup.txt")
    var = t.readlines()
    Text1 = var[0]
    Text2 = var[1]
    Text3 = var[2]

    # Initialize GUI
    app = Tracker(Text1, Text2, Text3)  
    app.mainloop()

I get the following box:

entry

My code is supposed to read in the data from the text file and display the entry boxes with pre-defined values from the text file. But it's acting funny. It doesn't save the data correctly

  1. I want this GUI to function so that when I edit the data in the entry boxes displayed above, it gets saved (on closing the session) and displayed automatically the next time I run it.

  2. Is there a way to do this in a loop so that I can display any number of entry boxes without having to hard-code the entry widgets?


Solution

  • You can easily create widgets in a loop. Strictly speaking you don't need to create the StringVars for each widget unless you really want to, because you can get and set the widget value using methods on each widget object.

    For example:

    import Tkinter as tk
    
    class SampleApp(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
            button = tk.Button(text="Save", command=self.save)
            button.pack(side="top")
            self.widgets = []
            for line in ["one","two","three","four"]:
                widget = tk.Entry(self)
                widget.insert(0, line)
                widget.pack(side="top", fill="x")
                self.widgets.append(widget)
    
        def save(self):
            for widget in self.widgets:
                print widget.get()
    
    if __name__ == "__main__":
        app = SampleApp()
        app.mainloop()