Working with GUIs and text files - how do I 'synchronize' them?
<p>I am working on a larger programming project, and since I am a beginner it is not that complex. I will try to keep it straight forward: I want to create a GUI program that reads from a text file with "elements of notes", i.e. as a calendar.</p>

<p>The interface should have "text entry box", where you can submit new notes, and there should also be two buttons that upon pressing them "scrolls" up and down among the existing notes, i.d. <em>turning page</em>. A single note should be displayed at all times in a text box under the two buttons.</p>

<p>So, to wrap this up, my question is: How is the best way to "load" the text file's notes to the program, so that I can make the buttons scroll between them? Should I read the text file into a list that I give my <code>Application(Frame)</code> object?</p>

<p>Here is some of my code so far:</p>

<pre><code>    from tkinter import *

    class Application(Frame):
        """ GUI application that creates diary. """
        def __init__(self, master):
            """ Initialize Frame. """
            Frame.__init__(self, master)
            self.grid()
            self.create_widgets()

        def create_widgets(self):
            """ Create widgets to get info of choices and display notes. """
            # create a label and text entry for new note
            Label(self,
                  text = "Enter new note:"
                  ).grid(row = 1, column = 0, sticky = W)
                  self.note_ent = Entry(self)
                  self.note_ent.grid(row = 1, column = 1, sticky = W)

            # create a submit button for the new note
            Button(self,
                   text = "Submit",
                   # command = self.add_note to a list within app obeject?
                   ).grid(row = 2 column = 0, sticky = W)

            # create a 'next note' button
            Button(self,
                   text = "Next",
                   # command = self.next_note which goes to a list?
                   ).grid(row = 6, column = 0, sticky = W)

            # create a 'past note' button
            Button(self,
                   text = "Back",
                   # command = self_past_note, or should I reuse next_note?
                   ).grid(row = 6, column = 0, sticky = W)

            # create a textbox (I am not sure?)
            self.show_ent = Text(self, width = 75, height = 10, wrap = WORD)
            self.show_ent.grid(row = 7, column = 0, columnspan = 4)

    # main
    text_file = open("diary.txt", "r")
    note_list = text_file.readlines()
    text_file.close()
    # No idea where to put the note_list, which 'client' should receive it?
    root = Tk()
    root.title("Diary")
    app = Application(root)
    root.mainloop()
</code></pre>

<p>So now that you have examined my code, how to fill in the missing pieces?</p>

<p><strong>Edit:</strong> I added the text_file and note_list under the # main.</p>

<p><strong>Note:</strong> I have used <em>calendar</em> and <em>diary</em> interchangeably, but the program is more of a calendar. </p>

<p>You can use it inside <code>Application</code> class:  </p>

<pre><code>from Tkinter import *

class Application(Frame):
        """ GUI application that creates diary. """
        def __init__(self):
            """ Initialize Frame. """
            Frame.__init__(self)

            self.note_list = []
            with open("diary.txt", "r") as notes:
                self.note_list = text_file.readlines()

            self.grid()
            self.create_widgets()

        def create_widgets(self):
            """ Create widgets to get info of choices and display notes. """
            # create a label and text entry for new note
            Label(self,
                  text = "Enter new note:"
                  ).grid(row = 1, column = 0, sticky = W)
            self.note_ent = Entry(self)
            self.note_ent.grid(row = 1, column = 1, sticky = W)

            # create a submit button for the new note
            Button(self,
                   text = "Submit",
                   # command = self.add_note to a list within app obeject?
                   ).grid(row = 2, column = 0, sticky = W)

            # create a 'next note' button
            Button(self,
                   text = "Next",
                   # command = self.next_note which goes to a list?
                   ).grid(row = 6, column = 0, sticky = W)

            # create a 'past note' button
            Button(self,
                   text = "Back",
                   # command = self_past_note, or should I reuse next_note?
                   ).grid(row = 6, column = 0, sticky = W)

            # create a textbox (I am not sure?)
            self.show_ent = Text(self, width = 75, height = 10, wrap = WORD)
            self.show_ent.grid(row = 7, column = 0, columnspan = 4)


app = Application()
app.master.title("Diary")
app.mainloop() 
</code></pre>

python oop user-interface tkinter text-files