#author:	Martin RobinSon
#alias:		Salopar
#title:		tkOhm.py
#project:	Monthly Python
#url:		monthlypython.blogspot.com

from math import *
from Tkinter import *
import tkSimpleDialog
import tkMessageBox

last, late = "empty", "empty"
tension = "Voltage"
courant = "Ampere"
resist = "Resistance"
power = "Puissance"

parent=Tk()

def law():
    global last
    global late
    E, I, P, R = volt.get(), amp.get(), watt.get(), ohm.get()
    #the ohm wheel
    if (last == power and late == tension) or (last == tension and late == power) :
        I = P/E
        R = (E*E)/P
    if (last == power and late == courant) or (last == courant and late == power) :
        R = P/(I*I)
        E = P/I
    if (last == power and late == resist) or (last == resist and late == power) :
        E = sqrt(P*R)
        I = sqrt(P/R)
    if (last == resist and late == courant) or (last == courant and late == resist) :
        E = I*R
        P = (I*I)*R
    if (last == courant and late == tension) or (last == tension and late == courant) :
        P = E*I
        R = E/I
    if (last == resist and late == tension) or (last == tension and late == resist) :
        P = (E*E)/R
        I = E/R
        
    late = last
    volt.set(E)
    amp.set(I)
    watt.set(P)
    ohm.set(R)

def enter_volt():
    global last     #python tend to create its own var instead of
    global late     #looking elsewhere if already present , global
                    #make sure python will try to find it
    var = tkSimpleDialog.askfloat(tension,tension,initialvalue="0.0")
			#a little box appear and ask to enter value then press enter
			#or to press cancel
    if var == None : var = 0.0
    volt.set(var)
    late = last
    last = tension

def enter_amp():
    global last 
    global late
    var = tkSimpleDialog.askfloat(courant,courant,initialvalue="0.0")
    if var == None : var = 0.0
    amp.set(var)
    late = last
    last = courant

def enter_watt():
    global last 
    global late
    var = tkSimpleDialog.askfloat(power,power,initialvalue="0.0")
    if var == None : var = 0.0
    watt.set(var)
    late = last
    last = power

def enter_ohm():
    global last 
    global late
    var = tkSimpleDialog.askfloat(resist,resist,initialvalue="0.0")
    if var == None : var = 0.0
    ohm.set(var)
    late = last
    last = resist

volt = DoubleVar()	#Tkinter variable object
button1 = Button(parent, text=tension, command = enter_volt).grid(row=1, column = 0)
label1 = Label(parent, textvariable=volt).grid(row=1, column = 2)

amp = DoubleVar()
button2 = Button(parent, text=courant, command = enter_amp).grid(row=2, column = 0)
label2 = Label(parent, textvariable=amp).grid(row=2, column = 2)

watt = DoubleVar()
button3 = Button(parent, text=power, command = enter_watt).grid(row=3, column = 0)
label3 = Label(parent, textvariable=watt).grid(row=3, column = 2)

ohm = DoubleVar()
button4 = Button(parent, text=resist, command = enter_ohm).grid(row=4, column = 0)
label4 = Label(parent, textvariable=ohm).grid(row=4, column = 2)

the_law = Button(parent, text="The Law", command = law).grid(row=0,column = 1)

def info():
	tkMessageBox.showinfo("about","The Ohm law is made in python coded by Martin RobinSon")
button_info = Button(parent, text="about",command = info).grid(row=0,column = 0)

parent.mainloop()
