使用GUI和文本文件 - 如何“同步”它们?

时间:2016-10-04 07:29:39

标签: python oop user-interface tkinter text-files

我正在开发一个更大的编程项目,因为我是初学者,所以并不复杂。我将尽力保持直截了当:我想创建一个GUI程序,从带有“注释元素”的文本文件中读取,即作为日历。

界面应该有“文本输入框”,您可以在其中提交新笔记,并且还应该有两个按钮,按下它们在现有笔记中向上和向下“滚动”,i.d。 翻页。应始终在两个按钮下的文本框中显示单个注释。

所以,为了解决这个问题,我的问题是:如何将文本文件的注释“加载”到程序中的最佳方法是什么,这样我可以让按钮在它们之间滚动?我应该将文本文件读入我提供Application(Frame)对象的列表吗?

以下是我目前的一些代码:

    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()

现在您已经检查了我的代码,如何填写缺失的部分?

编辑:我在#main下添加了text_file和note_list。

注意:我可以互换地使用日历日记,但程序更像是日历。

1 个答案:

答案 0 :(得分:0)

您可以在Application class:

中使用它
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()