0

I am new to python and trying to build a GUI that takes from date and to date from user and reads the data accordingly from a csv file and display the urine output (calculated in the csvimport fucntion). I also want to plot the graph for a specific time and urine output in that time.

Can anyone help me? My code so far is below and it isn't displaying any GUI. Please can anyone correct the basic errors and help me in running this?

            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()

2 Answers 2

1

You are calling takedate method as soon as you are initializing your class. Removing parentheses(which means, call the method) would solve your issue.

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

Your GUI doesn't show up because takedate method makes your program to wait for user input because of raw_input(..) calls.

You should consider using Entry instead of raw_input() for getting user input.

EDIT: You can put two Entry in your __init__ then use Entry's get method in takedate. Roughly something like below.

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()

Also, you should add self parameter when defining your method since it is part of that class.

def CsvImport(self, csv_file):
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I am confused as to where should i use Entry? in the takedate function? and how will I show that in the GUI if it is used in the takedate method?
hello. now when i enter the dates in the text field, the following error occurs: TypeError: CsvImport() takes 1 positional argument but 2 were given can you help me more in this?
@rushan When creating method, you should pass self as parameter since it's a part of your class. def CsvImport(self, csv_file):
1

If you not want to pass parameters into self.takedate() remove ()as below:

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

or change to

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

In this case you can pass e parameter to self.takedate(). Pass it to this maner:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.