2017-02-15 50 views
0

我創建了一個GUI,在該GUI中我讀取了一個CSV文件並計算了數據的液體輸出。現在,我希望做兩件事情:
1)我想基於時間生成輸出,就像日期
2)我想生成我的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="Count the file", command=self.takedate) 
     button3 = Button(self, text="Exit", command=master.destroy) 
     button1.grid() 
     button2.grid() 
     button3.grid() 
     self.userInputFromRaw = Entry(self) 
     self.userInputFromRaw.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() 

我怎樣才能實現上述2個任務?

+0

大多數人會使用MatPlotLib來做類似的事情。 –

+0

你能給我一個使用matplotlib和我的代碼的例子嗎?我是python的新手,所以無法弄清楚。 – rushan

回答

0

我必須同意Jacques de Hooge,你應該使用matplotlib。 在你的文件的beggining,導入:

import matplotlib.pyplot as plt 

當你只是想打開一個新窗口的情節,一個matplotlib窗口應該足夠了。您可以使用散點圖:

plt.scatter(X, Y) 

X是一個可重複的與x座標和Y一個可重複的與y座標。既然你想要做的事,在時間不斷的變化,你可以有一個values列表與值繪製,請執行下列操作:

plt.scatter(range(len(values)), values) 
plt.show() 

您可能還需要一個線程中運行這些,所以當matplotlib窗口打開時,程序的其餘部分不會「凍結」。有很多地方解釋這一點。

+0

感謝您的回答。我只是有一個更多的困惑,在X軸上,我想使用來自** takedate()**函數的日期,並且在y軸上我想打印來自** csvimport()函數的液體測量輸出。我應該在哪裏繪製圖表?在** __ init __ **函數中? – rushan