# L-31 MCS 260 Wed 30 Mar 2016 : guievaloo.py
"""
GUI to evaluate a user given function,
using a class.
"""

from tkinter import Tk, Label, Entry, Button
from tkinter import END, INSERT
from math import cos, sin

class EvalFun(object):
    """
    GUI to evaluate user given expressions.
    """
    def __init__(self, wdw):
        """
        Determines the layout of the GUI.
        """
        wdw.title("Evaluate Expressions")
        self.lb1 = Label(wdw, text="f(x) =")
        self.lb1.grid(row=0)
        self.lb2 = Label(wdw, text="x =")
        self.lb2.grid(row=0, column=2)
        self.fun = Entry(wdw)
        self.fun.grid(row=0, column=1)
        self.ent = Entry(wdw)
        self.ent.grid(row=0, column=3)
        self.res = Entry(wdw)
        self.res.grid(row=0, column=5)
        self.btt = Button(wdw, text="equals", \
            command=self.calc)
        self.btt.grid(row=0, column=4)

    def calc(self):
        """
        Evaluates the function f at x."
        """
        self.res.delete(0, END)
        x = float(self.ent.get())
        result = eval(self.fun.get())
        self.res.insert(INSERT, result)

def main():
    """
    Instantiates the EvalFun object
    and starts the event loop.
    """
    top = Tk()
    EvalFun(top)
    top.mainloop()

if __name__ == "__main__":
    main()
