# GUI4a.py
# Written by Todd Wareham for CS 2500
"""
Executes basic Python GUI (front end) with examples of various widgets in 
 nested frames, each with a different layout-type.

This version implements widget resizing under window size change.
"""

import sys
from Tkinter import *

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

## Root window setup / Global GUI data-variables

    root = Tk()
    var = {}
    numPrint = [1]
    numDisplay = [1]
    dv = StringVar()

## Callback functions

    def displayVar(title):
        dv.set("%s \"%s\" %f" % (title, var["Entry Text"].get(),
	                                var["Scale"].get()))
	numDisplay[0] += 1

    def printVar(title):
        print "%s \"%s\" %f" % (title, var["Entry Text"].get(),
	                               var["Scale"].get())
	numPrint[0] += 1

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

## Configure root window

        root.title(title)

## GUI widgets

## String-entry frame / widgets (Top)

        etf = Frame(root)
	etv = StringVar()
	etv.set("Initial Text String")
	Label(etf, text="Text Variable").pack(expand=YES, fill=BOTH, side=LEFT)
	Entry(etf, textvariable=etv).pack(expand=YES, fill=X, side=RIGHT)
	etf.pack(expand=YES, fill=BOTH, side=TOP)
	var["Entry Text"] = etv

## Scale-entry frame / widgets (Top)

	sf = Frame(root)
	sv = DoubleVar()
	Scale(label="Scale Demo", variable=sv, from_=0.0, to=4.0, 
	      tickinterval=1.0, resolution=0.5, showvalue=YES, 
	      orient='horizontal').pack(expand=YES, fill=BOTH, side=TOP)
	var["Scale"] = sv

## Control-button frame / widgets (Bottom)

        contf = Frame(root)
        Button(contf, text="Print\nVariables", command=(lambda : printVar("Print # %1d:" % (numPrint[0],)))).pack(expand=YES, fill=BOTH, side=LEFT)
        Button(contf, text="Display\nVariables", command=(lambda : displayVar("Display # %1d:" % (numDisplay[0],)))).pack(expand=YES, fill=BOTH, side=LEFT)
        Button(contf, text="Exit", command=root.quit).pack(expand=YES, fill=BOTH, side=RIGHT)
	contf.pack(expand=YES, fill=BOTH, side=BOTTOM)

## GUI variable value display frame / widgets (Bottom)

        df = Frame(root)
        Label(df, text="Variable Value Display Area").pack(expand=YES, fill=X, side=TOP)
        dv.set("No Display Activated")
        Label(df, textvariable=dv).pack(expand=YES, fill=X, side=TOP)
        df.pack(expand=YES, fill=BOTH, side=BOTTOM)

## Body of method runGUI: create GUI, execute GUI, and return GUI variable
##  values on GUI termination.

    makeGUI(title)
    root.mainloop()
    return (var["Entry Text"].get(), 
            var["Scale"].get())



if __name__ == "__main__":
    var = runGUI(sys.argv[0])
    print "Final Values: ", var

