2017-02-16 4 views
0

私はCSVファイルを読み込んでから、液体出力を計算するGUIを作成しました。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を使用することができます。あなたのApp.CsvImport機能では、あなたとprintへの呼び出しを置き換える必要があります:あなたはTkinterのテキストウィジェットにいくつかの参照が必要な場合

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

thisはここStackOverflowの上の様々な質問に伴い、助けになるかもしれません。


あなたが唯一の1行を表示しているので、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:Googleには「tkinterのテキストにテキストを挿入するだけで十分だろうと答えましたか?ウィジェット "または"マットプロットのプロットデータ "あなたは**常にStackOverflowで何かを尋ねる前にあなたが探しているものを**検索するべきです。あなたがそうしてはならない初心者であるからではありません。 – DCPY

関連する問題