#!/usr/bin/env python
# -*- coding: ascii -*-
from __future__ import print_function

# NOTICE... this file is generated by TkGridGUI.
# Any code or comments added by the user must be in designated areas ONLY.
# User additions will be lost if they are placed in code-generated areas.
# (i.e. Saving from TkGridGUI will over-write code-generated areas.)

# TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "imports"


from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
from builtins import object

from tkinter.ttk import Combobox, Progressbar, Separator, Treeview, Notebook

from tkinter import *
from tkinter import Button, Canvas, Checkbutton, Entry, Frame, Label, LabelFrame
from tkinter import Listbox, Message, Radiobutton, Spinbox, Text
from tkinter import OptionMenu
import tkinter.filedialog
from tkinter import _setit as set_command


# >>>>>>insert any user code below this comment for section "imports"
# Place any user import statements here
try:
    from rocketunits.rocket_units import *
except:
    from rocket_units import *
    

from decimal import Decimal, Context, ROUND_DOWN
eng_context = Context(prec=5, rounding=ROUND_DOWN)
sci_context = Context(prec=8, rounding=ROUND_DOWN)

float_formatD = {'general':'%G', 'engineering':'d',  'scientific':'s', 
                 'fixed float':'%12f', 'exponential':'%E'}
float_formatL = ['general', 'engineering',  'scientific', 
                 'fixed float', 'exponential']

# TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "top_of_init"
class _tk_rocket_units:
    def __init__(self, master):
    
        grid_frame = Frame( master )
        self.grid_frame = grid_frame
        grid_frame.pack(expand=1, fill=BOTH)
        self.master = master
        
        self.x, self.y, self.w, self.h = 10, 10, 403, 513

        self.master.title("tk_rocket_units")

        self.make_Frame_1( self.grid_frame )           #       Frame:  at Main(1,1)
        self.make_Label_10( self.grid_frame )          #       Label:  at Main(0,0)
        self.make_Label_11( self.grid_frame )          #       Label:  at Main(4,1)
        self.make_Label_7( self.grid_frame )           #       Label:  at Main(2,1)
        self.make_Label_8( self.grid_frame )           #       Label:  at Main(1,0)
        self.make_Label_9( self.grid_frame )           #       Label:  at Main(1,2)
        self.make_Text_1( self.grid_frame )            #        Text:  at Main(3,1)
        self.make_Combobox_1( self.Frame_1 )           #    Combobox: Mine Yours Ours : at Frame_1(1,1)
        self.make_Combobox_2( self.Frame_1 )           #    Combobox: Mine Yours Ours : at Frame_1(3,1)
        self.make_Combobox_3( self.Frame_1 )           #    Combobox: Mine Yours Ours : at Frame_1(6,3)
        self.make_Entry_1( self.Frame_1 )              #       Entry:  at Frame_1(5,1)
        self.make_Label_1( self.Frame_1 )              #       Label: Category : at Frame_1(1,2)
        self.make_Label_2( self.Frame_1 )              #       Label: Units : at Frame_1(3,2)
        self.make_Label_3( self.Frame_1 )              #       Label: Value : at Frame_1(5,2)
        self.make_Label_4( self.Frame_1 )              #       Label: Number Format : at Frame_1(5,3)
        self.make_Label_5( self.Frame_1 )              #       Label:  at Frame_1(2,1)
        self.make_Label_6( self.Frame_1 )              #       Label:  at Frame_1(4,1)

        self.grid_frame.rowconfigure(3, weight=1)
        self.grid_frame.columnconfigure(1, weight=1)

        # >>>>>>insert any user code below this comment for section "top_of_init"
        
        self.show_all_units()


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Frame_1"
    def make_Frame_1(self, frame):
        """       Frame:  at Main(1,1)"""
        self.Frame_1 = Frame( frame , width="60", height="50", borderwidth="0", relief="flat")
        self.Frame_1.grid(row=1, column=1, sticky="w")

        # >>>>>>insert any user code below this comment for section "make_Frame_1"

    def show_all_units(self):
        self.Text_1.delete(1.0,END)
        
        cat = self.Combobox_1_StringVar.get()
        inp_units = self.Combobox_2_StringVar.get()
        fmt = float_formatD[ self.Combobox_3_StringVar.get() ]

        try:
            inp_val = float( self.Entry_1_StringVar.get() )
        except:
            inp_val = 0.0
            
        def_unit_val = (inp_val-offsetD[inp_units]) / conv_factD[inp_units]
        
        for units in categoryD[cat]:
            val = def_unit_val* conv_factD[units] + offsetD[units]
            
            if fmt == 'd':
                dval = eng_context.create_decimal_from_float( val )
                sOut = dval.to_eng_string()
            elif fmt == 's':
                dval = sci_context.create_decimal_from_float( val )
                sOut = str( dval.normalize() )
            else:
                sOut = fmt%val
            self.Text_1.insert(END, UNIT_FMT_STR%units + " = " + sOut + '\n')
            


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_10"
    def make_Label_10(self, frame):
        """       Label:  at Main(0,0)"""
        self.Label_10 = Label( frame , text="", width="4")
        self.Label_10.grid(row=0, column=0)

        # >>>>>>insert any user code below this comment for section "make_Label_10"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_11"
    def make_Label_11(self, frame):
        """       Label:  at Main(4,1)"""
        self.Label_11 = Label( frame , text="", width="4")
        self.Label_11.grid(row=4, column=1)

        # >>>>>>insert any user code below this comment for section "make_Label_11"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_7"
    def make_Label_7(self, frame):
        """       Label:  at Main(2,1)"""
        self.Label_7 = Label( frame , text="", width="15")
        self.Label_7.grid(row=2, column=1)

        # >>>>>>insert any user code below this comment for section "make_Label_7"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_8"
    def make_Label_8(self, frame):
        """       Label:  at Main(1,0)"""
        self.Label_8 = Label( frame , text="", width="4")
        self.Label_8.grid(row=1, column=0)

        # >>>>>>insert any user code below this comment for section "make_Label_8"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_9"
    def make_Label_9(self, frame):
        """       Label:  at Main(1,2)"""
        self.Label_9 = Label( frame , text="", width="4")
        self.Label_9.grid(row=1, column=2)

        # >>>>>>insert any user code below this comment for section "make_Label_9"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Text_1"
    def make_Text_1(self, frame):
        """        Text:  at Main(3,1)"""
        self.Text_1 = Text( frame , width="40", height="20")
        self.Text_1.grid(row=3, column=1, sticky="nsew")

        # >>>>>>insert any user code below this comment for section "make_Text_1"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Combobox_1"
    def make_Combobox_1(self, frame):
        """    Combobox: Mine Yours Ours : at Frame_1(1,1)"""
        self.Combobox_1 = Combobox( frame , values="Mine Yours Ours", text="Combobox_1")
        self.Combobox_1.grid(row=1, column=1, sticky="e")
        self.Combobox_1_StringVar = StringVar()

        # >>>>>>insert any user code below this comment for section "make_Combobox_1"

        self.Combobox_1.configure(textvariable=self.Combobox_1_StringVar)
        self.Combobox_1_StringVar.set( "Length" )
        self.Combobox_1['height'] = len( categoryL )
        self.Combobox_1['values'] = categoryL

        self.Combobox_1_StringVar_traceName = self.Combobox_1_StringVar.trace_variable("w", self.Combobox_1_StringVar_Callback)

    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Combobox_2"
    def make_Combobox_2(self, frame):
        """    Combobox: Mine Yours Ours : at Frame_1(3,1)"""
        self.Combobox_2 = Combobox( frame , values="Mine Yours Ours", text="Combobox_2")
        self.Combobox_2.grid(row=3, column=1, sticky="e")
        self.Combobox_2_StringVar = StringVar()

        # >>>>>>insert any user code below this comment for section "make_Combobox_2"


        self.Combobox_2.configure(textvariable=self.Combobox_2_StringVar)
        self.Combobox_2_StringVar.set( "ft" )
        self.Combobox_2['height'] = 20
        self.Combobox_2['values'] = categoryD[ self.Combobox_1_StringVar.get() ]

        self.Combobox_2_StringVar_traceName = self.Combobox_2_StringVar.trace_variable("w", self.Combobox_2_StringVar_Callback)

    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Combobox_3"
    def make_Combobox_3(self, frame):
        """    Combobox: Mine Yours Ours : at Frame_1(6,3)"""
        self.Combobox_3 = Combobox( frame , values="Mine Yours Ours", text="Combobox_3")
        self.Combobox_3.grid(row=6, column=3, sticky="e")
        self.Combobox_3_StringVar = StringVar()

        # >>>>>>insert any user code below this comment for section "make_Combobox_3"

        self.Combobox_3.configure(textvariable=self.Combobox_3_StringVar)
        self.Combobox_3_StringVar.set( "general" )
        self.Combobox_3['height'] = len( float_formatD )
        self.Combobox_3['values'] = list( float_formatL )
        self.Combobox_3_StringVar_traceName = self.Combobox_3_StringVar.trace_variable("w", self.Combobox_3_StringVar_Callback)

    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Entry_1"
    def make_Entry_1(self, frame):
        """       Entry:  at Frame_1(5,1)"""
        self.Entry_1 = Entry( frame , width="13")
        self.Entry_1.grid(row=5, column=1, sticky="e")
        self.Entry_1_StringVar = StringVar()

        # >>>>>>insert any user code below this comment for section "make_Entry_1"
        self.Entry_1_StringVar.set("1.0")

        self.Entry_1.configure(textvariable=self.Entry_1_StringVar)
        self.Entry_1_StringVar_traceName = self.Entry_1_StringVar.trace_variable("w", self.Entry_1_StringVar_Callback)

    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_1"
    def make_Label_1(self, frame):
        """       Label: Category : at Frame_1(1,2)"""
        self.Label_1 = Label( frame , text="Category", width="15", anchor="w")
        self.Label_1.grid(row=1, column=2, sticky="w")

        # >>>>>>insert any user code below this comment for section "make_Label_1"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_2"
    def make_Label_2(self, frame):
        """       Label: Units : at Frame_1(3,2)"""
        self.Label_2 = Label( frame , text="Units", width="15", anchor="w")
        self.Label_2.grid(row=3, column=2, sticky="w")

        # >>>>>>insert any user code below this comment for section "make_Label_2"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_3"
    def make_Label_3(self, frame):
        """       Label: Value : at Frame_1(5,2)"""
        self.Label_3 = Label( frame , text="Value", width="15", anchor="w")
        self.Label_3.grid(row=5, column=2, sticky="w")

        # >>>>>>insert any user code below this comment for section "make_Label_3"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_4"
    def make_Label_4(self, frame):
        """       Label: Number Format : at Frame_1(5,3)"""
        self.Label_4 = Label( frame , text="Number Format", width="15")
        self.Label_4.grid(row=5, column=3)

        # >>>>>>insert any user code below this comment for section "make_Label_4"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_5"
    def make_Label_5(self, frame):
        """       Label:  at Frame_1(2,1)"""
        self.Label_5 = Label( frame , text="", width="15")
        self.Label_5.grid(row=2, column=1)

        # >>>>>>insert any user code below this comment for section "make_Label_5"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "make_Label_6"
    def make_Label_6(self, frame):
        """       Label:  at Frame_1(4,1)"""
        self.Label_6 = Label( frame , text="", width="15")
        self.Label_6.grid(row=4, column=1)

        # >>>>>>insert any user code below this comment for section "make_Label_6"


    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "Combobox_1_StringVar_traceName"
    def Combobox_1_StringVar_Callback(self, varName, index, mode):
        """    Combobox: Mine Yours Ours : at Frame_1(1,1)"""
        pass

        # >>>>>>insert any user code below this comment for section "Combobox_1_StringVar_traceName"
        # replace, delete, or comment-out the following
        self.Combobox_2['values'] = categoryD[ self.Combobox_1_StringVar.get() ]
        self.Combobox_2.current(0)
        
        self.show_all_units()



    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "Combobox_2_StringVar_traceName"
    def Combobox_2_StringVar_Callback(self, varName, index, mode):
        """    Combobox: Mine Yours Ours : at Frame_1(3,1)"""
        pass

        # >>>>>>insert any user code below this comment for section "Combobox_2_StringVar_traceName"
        # replace, delete, or comment-out the following
        self.show_all_units()



    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "Combobox_3_StringVar_traceName"
    def Combobox_3_StringVar_Callback(self, varName, index, mode):
        """    Combobox: Mine Yours Ours : at Frame_1(6,3)"""
        pass

        # >>>>>>insert any user code below this comment for section "Combobox_3_StringVar_traceName"
        # replace, delete, or comment-out the following
        self.show_all_units()



    # TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "Entry_1_StringVar_traceName"
    def Entry_1_StringVar_Callback(self, varName, index, mode):
        """       Entry:  at Frame_1(5,1)"""
        pass

        # >>>>>>insert any user code below this comment for section "Entry_1_StringVar_traceName"
        # replace, delete, or comment-out the following
        self.Combobox_2['values'] = categoryD[ self.Combobox_1_StringVar.get() ]
        #self.Combobox_2.current(0)
        self.show_all_units()



# TkGridGUI generated code. DO NOT EDIT THE FOLLOWING. section "end"

def main():
    root = Tk()
    app = _tk_rocket_units(root)
    root.mainloop()

if __name__ == '__main__':
    main()
