# expfltGUI2.py
# Written by Todd Wareham for CS 2500
"""
Compute and print the floating-point values of string-representations of 
 exponential numbers obtained from a GUI.

Note that we assume that this esxponential number is in the range of 
 floating-point values.
"""

import sys 
import re
from Tkinter import *


def expNumVal(expNumStr):
    mantissa = r"([+\-]?\d\.\d+)"
    exponent = r"([+\-]\d+)"
    r = re.compile(mantissa + "[eE]" + exponent)
    m = r.match(expNumStr)
    return float(m.group(1)) * pow(10, int(m.group(2)))


def runGUI(title):
## Create and execute GUI with specified title

## Root window setup / Global GUI data-variables

    root = Tk()
    env = StringVar()
    efv = StringVar()

## Callback functions

    def procNumStr():
        efv.set(str(expNumVal(env.get())))
        return


    def makeGUI(title):
##  Set up appearance and behavior of GUI with specified title

## Configure root window

        root.title(title)

## GUI widgets

## Number-entry frame / widgets (Top)

        entf = Frame(root)
	env.set("--")
	Label(entf, text="Exponential Number: ").pack(expand=YES, fill=BOTH, side=LEFT)
	Entry(entf, textvariable=env).pack(expand=YES, fill=X, side=RIGHT)
	entf.pack(expand=YES, fill=BOTH, side=TOP)

## Number-value display frame / widgets (Top)

        dispf = Frame(root)
	Label(dispf, text="Floating-point value = ").pack(expand=YES, fill=BOTH, side=LEFT)
	efv.set("*")
	Label(dispf, textvariable=efv).pack(expand=YES, fill=BOTH, side=LEFT)
	dispf.pack(expand=YES, fill=BOTH, side=TOP)

## Control-button frame . widgets (Bottom)

        contf = Frame(root)
        Button(contf, text="Process\nNumber", command=procNumStr).pack(expand=YES, fill=BOTH, side=LEFT)
        Button(contf, text="Exit", command=sys.exit).pack(expand=YES, fill=BOTH, side=RIGHT)
	contf.pack(expand=YES, fill=BOTH, side=BOTTOM)


## Body of method runGUI: create and execute GUI, terminating script on GUI
##  termination.

    makeGUI(title)
    root.mainloop()



if __name__ == "__main__":

    runGUI(sys.argv[0])

