# -*- coding: utf-8 -*- # Calculate commute costs from Tkinter import * from decimal import * def calculateCommuteCost(): distance = Decimal(gui.distanceToWorkBox.get()) mpg = Decimal(gui.milesPerGallonBox.get()) ppg = Decimal(gui.pricePerGallonBox.get()) costpertrip = (distance / mpg) * ppg #cost per month: assuming you work 5 days per week #have two weeks vacation #and make one trip back and forth each of those day costpermonth = costpertrip * ((Decimal("365.25")-14)*5/7/12*2) costpermonth = round(costpermonth,2) costpertrip = round(costpertrip,2) gui.commuteCost.set("Commute Cost Per Trip: $%0.2f" % costpertrip) gui.commuteCostPerMonth.set("Average Commute Cost Per Month: $%0.2f" % costpermonth) class GUI: def __init__(self, master): #frame = Frame(master) #frame.pack() #self.button = Button(frame, text="Test", command=self.test) #self.button.pack() self.commuteCost = StringVar() self.commuteCost.set("Commute Cost Per Trip: $0.00") self.commuteCostPerMonth = StringVar() self.commuteCostPerMonth.set("Average Commute Cost Per Month: $000.00") self.distanceToWorkLabel = Label(master, text="Miles to Work").pack() self.distanceToWorkBox = Entry(master) self.distanceToWorkBox.insert(END, "30") self.distanceToWorkBox.pack() self.milesPerGallonLabel = Label(master, text="Miles Per Gallon").pack() self.milesPerGallonBox = Entry(master) self.milesPerGallonBox.insert(END, "20") self.milesPerGallonBox.pack() self.pricePerGallonLabel = Label(master, text="Price per gallon").pack() self.pricePerGallonBox = Entry(master) self.pricePerGallonBox.insert(END, "3.15") self.pricePerGallonBox.pack() self.commuteCostLabel = Label(master, textvariable=self.commuteCost).pack() self.commuteCostPerMonthLabel = Label(master, textvariable=self.commuteCostPerMonth).pack() #distance to work, miles per gallon, gas cost per gallon self.calculateButton = Button(master, text="Calculate Commute Cost", command=calculateCommuteCost).pack() root = Tk() gui = GUI(root) root.mainloop()