[英]Python GUI and function calling using Tkinter
我是python的新手,正在尝试构建一个从用户获取日期和日期的GUI,并从csv文件中相应地读取数据并显示尿液输出(以csvimport功能计算)。 我还想绘制一个特定时间的图形以及该时间的尿量。
谁能帮我? 到目前为止,我的代码在下面,并且没有显示任何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()
初始化类后,您将立即调用 takedate
方法。 删除括号(这意味着调用方法)将解决您的问题。
button2 = Button(self, text="Count the file", command=self.takedate())
^^ remove these
您的GUI不会显示,因为takedate
方法使您的程序由于raw_input(..)
调用而等待用户输入。
您应该考虑使用Entry
而不是raw_input()
来获取用户输入。
编辑:您可以在__init__
放入两个Entry,然后在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()
同样,定义方法时应添加self参数,因为它是该类的一部分。
def CsvImport(self, csv_file):
如果您不想将参数传递到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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.