我是python的新手,并尝试构建一个从用户日期和日期开始的GUI,并从csv文件中相应地读取数据并显示尿液输出(在csvimport fucntion中计算)。我还想绘制特定时间的图表和那段时间的尿量。
任何人都可以帮助我吗?到目前为止我的代码在下面,它没有显示任何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()
答案 0 :(得分:1)
初始化课程时,您正在调用 takedate
方法。删除括号(表示调用方法)可以解决您的问题。
button2 = Button(self, text="Count the file", command=self.takedate())
^^ remove these
您的GUI未显示,因为takedate
方法会使您的程序因raw_input(..)
来电而等待用户输入。
您应该考虑使用Entry
代替raw_input()
来获取用户输入。
编辑:您可以在__init__
中添加两个条目,然后在takedate
中使用条目的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()
此外,您应该在定义方法时添加self参数,因为它是该类的一部分。
def CsvImport(self, csv_file):
答案 1 :(得分: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