2016-10-04 65 views
0

我工作的一個更大的編程項目,因爲我是一個初學者它不是那麼複雜。我會盡量保持直線前進:我想創建一個GUI程序,從「票據要素」,即作爲日曆一個文本文件中讀取。使用圖形用戶界面和文本文件 - 我如何「同步」它們?

接口應該有「文本輸入框」,在這裏你可以提交新的筆記,還應該有兩個按鈕,在按下他們「滾動」上下現有的筆記中,內徑轉向頁。一個音符應該始終顯示在兩個按鈕下的文本框中。

因此,包裝這件事,我的問題是:如何是最好的方式,以「裝載」文本文件的筆記程序,這樣我就可以使按鈕它們之間滾動?我是否應該將文本文件讀入列表中,我給出了我的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。

注:我用日曆日記互換,但程序更是一個日曆。

+0

如果你使用的類,那麼你可以在'__init__'做到這一點。 – furas

+0

「不知道放在哪裏note_list」把它在你需要它。 – Goyo

回答

0

你可以使用它裏面Application類:

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

謝謝,它現在有效。 – SimpleProgrammer