2016-03-31 23 views
0

Yeni girdileri veri listesine nasıl kaydederim?Python: Veri listeme ek girişler nasıl kaydedilir?

Şu anda, giriş kutusuna metin girdiğimde, listemde görüntülenir: veri. Ancak, giriş kutusunu temizlediğimde ve yeni bir değer eklediğimde, bu değer veri listesine eklenirken, daha önce girilen değer kaybolur. Her bir katmanın listede kalmasını istiyorum. Python'a yeni ve genel olarak kodlama yapıyorum. Tüm yardımlar büyük beğeni topluyor!

import Tkinter as tk 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 

     # create a prompt, an input box, an output label, 
     # and a button to do the saving 
     self.prompt = tk.Label(self, text="Enter your Filepath:", anchor="w") 
     self.entry = tk.Entry(self) 
     self.submit = tk.Button(self, text="Save", command = self.save) 
     self.output = tk.Label(self, text="") 
     self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text) 

     # lay the widgets out on the screen. 
     self.prompt.pack(side="top", fill="x") 
     self.entry.pack(side="top", fill="x", padx=20) 
     self.output.pack(side="top", fill="x", expand=True) 
     self.submit.pack(side="bottom") 
     self.clear_button.pack(side="bottom") 


    def clear_text(self): 
     self.entry.delete(0, 'end') 

    def save(self): 
     # get the value from the input widget, save 
     # it to list data 
     data = [] 

     try: 
      a = self.entry.get() 
      result = data.append(a) 

     except ValueError: 
      result = "Please enter filepaths only" 

     # set the output widget to have our result 
     self.output.configure(text=result) 
     print (data) 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
+1

'data' yerel değişkendir ve boş bir listeye sen gönder düğmesine basın her zaman yeniden ediliyor (Kod aşağıda gösterilmiştir). Öyleyse, değerlerin “ortadan kaybolan” olmasının iki nedeni var. 1: 'data' yerel bir değişkendir, bu yüzden' save' fonksiyonundan çıktığınızda silinir. 2: 'Kaydet'i çağırdığınızda, 'data = []' – Dzhao

+0

ile elinizin üzerine yazabilirsiniz. Verileri kaydetme işlevi dışında taşıdım, ancak program yine de listeyi oluşturmuyor. Bu sorunu nasıl çözmeliyim? – mattyb

+0

Sorguyu gözden geçirilmiş kodunuzla güncelleyin. – Dzhao

cevap

0

Çözüm ...

import Tkinter as tk 
data = [] 
class Example(tk.Frame): 


def __init__(self, parent): 
    tk.Frame.__init__(self, parent) 

    # create a prompt, an input box, an output label, 
    # and a button to do the computation 
    self.prompt = tk.Label(self, text="Enter your Filepath:", anchor="w") 
    self.entry = tk.Entry(self) 
    self.submit = tk.Button(self, text="Save", command = self.save) 
    self.output = tk.Label(self, text="") 
    self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text) 

    # lay the widgets out on the screen. 
    self.prompt.pack(side="top", fill="x") 
    self.entry.pack(side="top", fill="x", padx=20) 
    self.output.pack(side="top", fill="x", expand=True) 
    self.submit.pack(side="bottom") 
    self.clear_button.pack(side="bottom") 


def clear_text(self): 
    self.entry.delete(0, 'end') 


def save(self): 
    # get the value from the input widget, convert 
    # it to an int, and do a calculation 

    try: 
     a = self.entry.get() 
     result = data.append(self.entry.get()) 

    except ValueError: 
     result = "Please enter filepaths only" 


    # set the output widget to have our result 
    self.output.configure(text=result) 
    print (data) 


if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
İlgili konular