2017-02-15 41 views
0

我是python的新手,並試圖構建一個GUI,它從用戶的日期和日期中讀取數據,並相應地從csv文件中讀取數據並顯示尿液輸出(在csvimport函數中計算)。我也想在那段時間繪製特定時間和尿量的圖表。使用Tkinter的Python GUI和函數調用

任何人都可以幫助我嗎?我的代碼到目前爲止在下面,它不顯示任何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) 
        self.in_file = None 
        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.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(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 = raw_input('\nEnter FROM Date (e.g. 2013-11-29) :') 
        from_date = datetime.date(*map(int, from_raw.split('/'))) 
        print ('From date: = ' + str(from_date)) 
        to_raw = raw_input('\nEnter TO Date (e.g. 2013-11-30) :') 
        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() 

回答

1

儘快調用takedate方法要初始化類。刪除括號(意思是,調用該方法)將解決您的問題。

button2 = Button(self, text="Count the file", command=self.takedate()) 
                    ^^ remove these 

您的GUI不會顯示出來,因爲takedate方法使你的程序等待用戶輸入,因爲raw_input(..)電話。

您應該考慮使用Entry而不是raw_input()來獲取用戶輸入。

編輯:你可以把兩個條目放入你的__init__然後在takedate中使用Entry的get方法。大致如下所示。

def __init__(self, master): 
    ... 
    ... 
    self.userInputFromRaw = Entry(self) 
    self.userInputFromRaw.grid() 

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

def takedate(self): 
    ... 
    from_raw = self.userInputFromRaw.get() 
    ... 
    to_raw = self.userInputToRaw.get() 

另外,定義方法時應該添加自參數,因爲它是該類的一部分。

def CsvImport(self, csv_file): 
+0

感謝您的回答。我很困惑,我應該在哪裏使用** Entry **?在** takedate **功能?以及如何在GUI中使用** takedate **方法顯示該圖像? – rushan

+0

@rushan增加了一些代碼。 – Lafexlos

+0

你好。現在,當我在文本字段中輸入日期時,會發生以下錯誤:** TypeError:CsvImport()需要1個位置參數,但是有2個被給出**您能幫助我更多嗎? – rushan

1

如果不希望將參數傳遞到self.takedate()如下刪除()

button2 = Button(self, text="Count the file", command=self.takedate) 

或更改

button2 = Button(self, text="Count the file", command=lambda e=Null: self.takedate()) 

在這種情況下,你可以通過e參數self.takedate()。將其傳遞給:

command=lambda e=Null: self.takedate(e) 
def takedate(self, parameter): pass