2017-02-16 37 views
0

我創建了一個GUI,在該GUI中我讀取CSV文件,然後計算液體輸出。在CSVImport函數中,有一個print語句,其中計算輸出。我想在我的GUI中使用文本小部件打印。我怎樣才能做到這一點?我的代碼如下:如何在函數中將值顯示到Python中的GUI小部件?

import csv 
from tkinter import * 
from tkinter.filedialog import askopenfilename 
from tkinter.messagebox import showwarning, showinfo 
import datetime 

#csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv")) 
from Tools.scripts.treesync import raw_input 
class App(Frame): 
    def __init__(self, master): 
     Frame.__init__(self, master) 


     button1 = Button(self, text="Browse for a file", command=self.askfilename) 
     button2 = Button(self, text="Measure The Urine", command=self.takedate) 
     button3 = Button(self, text="Exit", command=master.destroy) 
     button1.grid() 
     button2.grid() 
     button3.grid() 
     l1 = Label(self, text="Enter from date (2017/01/01)") 
     l1.grid() 

     self.userInputFromRaw = Entry(self) 
     self.userInputFromRaw.grid() 
     l2 = Label(self, text="Enter to date (2017/01/01)") 

     l2.grid() 
     self.userInputToRaw = Entry(self) 
     self.userInputToRaw.grid() 



     self.grid() 

    def askfilename(self): 
     in_file = askopenfilename() 
     if not in_file.endswith(('.CSV')): 
      showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?') 
     else: 
      self.in_file=in_file 

    def CsvImport(self,csv_file): 


     dist = 0 
     for row in csv_file: 
      _dist = row[0] 
      try: 
       _dist = float(_dist) 
      except ValueError: 
       _dist = 0 

      dist += _dist 
     print ("Urine Volume is: %.2f" % (_dist*0.05)) 


    def takedate(self): 
     from_raw = self.userInputFromRaw.get() 
     from_date = datetime.date(*map(int, from_raw.split('/'))) 
     print ('From date: = ' + str(from_date)) 
     to_raw = self.userInputToRaw.get() 
     to_date = datetime.date(*map(int, to_raw.split('/'))) 
     in_file = ("H:\DataLog.csv") 
     in_file= csv.reader(open(in_file,"r")) 

     for line in in_file: 
      _dist = line[0] 
      try: 
       file_date = datetime.date(*map(int, line[1].split(' ')[1].split('/'))) 
       if from_date <= file_date <= to_date: 
        self.CsvImport(in_file) 

      except IndexError: 
       pass 

root = Tk() 
root.title("Urine Measurement") 
root.geometry("500x500") 
app = App(root) 
root.mainloop() 

回答

0

首先,你必須有一個文本部件。在你App.__init__功能,你應該添加像下面這樣:

self.output = Text(self) 
self.output.grid() 

將文本追加到小部件,您可以使用Text.insert;如果您需要在Tkinter的文本控件,this一些參考可能的幫助,在這裏StackOverflow上的各種問題以及

self.output.insert(END, "Urine Volume is: %.2f" % (_dist*0.05)) 

:在你的App.CsvImport功能,則應更換呼叫print用。


這也是值得注意的是,因爲你是隻顯示一條線,這可能是使用Label更方便。正常創建標籤,當你想改變它的文本,你可以使用:

self.output["text"] = "Urine Volume is: %.2f" % (_dist*0.05) 
+0

偉大的工作就像一個魅力。你也可以回答[http://stackoverflow.com/questions/42252096/creating-graphs-in-python-gui] – rushan

+0

@rushan:也回答了,即使它很容易谷歌「插入文本到tkinter文本小部件「或」matplotlib繪圖數據「。您應該**總是**在StackOverflow上詢問某些內容之前搜索您要查找的內容。這不是因爲你是初學者,你不應該這樣做。 – DCPY

相關問題