if __name__ == "__main__": exit("Oops! Don't try to launch me!") from Tkinter import * from pccMathModule import * class SimpleWindow(Frame): def __init__(self, master=None): Frame.__init__(self, master) # You can relabel this window anything you like! self.master.title("Exponential Calculator") # Specify that we'll be using the "grid" layout manager self.grid() # Error console or whatever short message we want to print self.console = Entry(self, justify=CENTER, relief=FLAT) self.console.delete(0, END) self.console.insert(0, "Formula: A(t) = A(0) * (b) ^ (t/T)") self.console.grid(row = 0, column = 0, columnspan = 2, sticky=N+E+S+W) # Labels and text fields self.Linitial = Label(self, text="Initial Quantity A(0)") self.Linitial.grid(row = 1, column = 0, sticky=E) self.Einitial = Entry(self) self.Einitial.grid(row = 1, column = 1) self.Lbase = Label(self, text="Base (b)") self.Lbase.grid(row = 2, column = 0, sticky=E) self.Ebase = Entry(self) self.Ebase.grid(row = 2, column = 1) self.Ltime = Label(self, text="Time (t)") self.Ltime.grid(row = 3, column = 0, sticky=E) self.Etime = Entry(self) self.Etime.grid(row = 3, column = 1) self.Lperiod = Label(self, text="Period (T)") self.Lperiod.grid(row = 4, column = 0, sticky=E) self.Eperiod = Entry(self) self.Eperiod.grid(row = 4, column = 1) self.Lquantity = Label(self, text="Final Quantity A(t)") self.Lquantity.grid(row = 5, column = 0, sticky=E) self.Equantity = Entry(self) self.Equantity.grid(row = 5, column = 1) # Control panel -- buttons... self.calcButton = Button(self, text='Solve Now!', command=self.Calc) self.calcButton.grid(row = 6, column = 0, columnspan = 2) # Invisible -- Bind all text entry / button fields to the return key. def ReturnHandler(eventObj): self.Calc() self.bind_class("Entry", "", ReturnHandler) self.bind_class("Button", "", ReturnHandler) # Invisible -- Bind all to escape key. def EscHandler(eventObj): quit() self.bind_class("Entry", "", EscHandler) self.bind_class("Button", "", EscHandler) def Calc(self): Vinitial = self.Einitial.get() Vbase = self.Ebase.get() Vtime = self.Etime.get() Vperiod = self.Eperiod.get() Vquantity = self.Equantity.get() def OopsEmptyFields(): self.console.delete(0, END) self.console.insert(0, "More than one empty field!") def OopsWrongData(whatField): self.console.delete(0, END) self.console.insert(0, "Not a real number: " + whatField) def OopsNoEmptyFields(): self.console.delete(0, END) self.console.insert(0, "Nothing to solve!") getWhat = None # Get or Find Initial Quantity Finitial = None if len(Vinitial) == 0: getWhat = "initial" else: try: Finitial = float(Vinitial) except ValueError: OopsWrongData("Initial Quantity") return # Base Fbase = None if len(Vbase) == 0: if getWhat == None: getWhat = "base" else: OopsEmptyFields() return else: try: Fbase = float(Vbase) except ValueError: OopsWrongData("Base") return # Time Ftime = None if len(Vtime) == 0: if getWhat == None: getWhat = "time" else: OopsEmptyFields() return else: try: Ftime = float(Vtime) except ValueError: OopsWrongData("Time") return # Period Fperiod = None if len(Vperiod) == 0: if getWhat == None: getWhat = "period" else: OopsEmptyFields() return else: try: Fperiod = float(Vperiod) except ValueError: OopsWrongData("Period") return # Final Quantity Fquantity = None if len(Vquantity) == 0: if getWhat == None: getWhat = "quantity" else: OopsEmptyFields() return else: try: Fquantity = float(Vquantity) except ValueError: OopsWrongData("Final Quantity") return # Make a new MathGuts object to store everything-- and solve! mg = MathGuts(Finitial, Fbase, Ftime, Fperiod, Fquantity) if getWhat == None: OopsNoEmptyFields() return elif getWhat == "initial": self.Einitial.delete(0, END) self.Einitial.insert(0, str(mg.calcA0())) elif getWhat == "base": self.Ebase.delete(0, END) self.Ebase.insert(0, str(mg.calcb())) elif getWhat == "time": self.Etime.delete(0, END) self.Etime.insert(0, str(mg.calct())) elif getWhat == "period": self.Eperiod.delete(0, END) self.Eperiod.insert(0, str(mg.calcT())) elif getWhat == "quantity": self.Equantity.delete(0, END) self.Equantity.insert(0, str(mg.calcAt())) # Reset text at the top back to normal... self.console.delete(0, END) self.console.insert(0, "Formula: A(t) = A(0) * (b) ^ (t/T)") # Substance remaining in exponential decay/growth: # [] : quantity(0), base, time, period -> quantity(t) # example: half-life decay: base = 1/2, period = half-life # example: doubling population: base = 2, period = generation time