python作业–GUI小作业
题目
编写程序计算在给定利率、指定年数的情况下投资的未来值。这个计算公式如下。
使用文本域输入投资额、年份和利率。当用户单击“calculate”按钮时,在文本域中显示未来的投资值
futureVaule = inverestmentAmount * (1 + monthlyInterestRate) ** (years * 12)
解
from tkinter import *
class InterestCal:
def __init__(self):
window = Tk()
window.title("Investment Calculator")
Label(window, text="Investment Amount").grid(row=1, column=1, sticky=W)
Label(window, text="Years").grid(row=2, column=1, sticky=W)
Label(window, text="Annual Interest Rate").grid(row=3, column=1, sticky=W)
Label(window, text="Future Value").grid(row=4, column=1, sticky=W)
self.investmentAmount = DoubleVar()
Entry(window, textvariable=self.investmentAmount, justify=RIGHT).grid(row=1, column=2)
self.years = IntVar()
Entry(window, textvariable=self.years, justify=RIGHT).grid(row=2, column=2)
self.annualInterestRate = DoubleVar()
Entry(window, textvariable=self.annualInterestRate, justify=RIGHT).grid(row=3, column=2)
self.futureValue = DoubleVar()
lblresult = Label(window, textvariable=self.futureValue).grid(row=4, column=2, sticky=E)
btCompute = Button(window, text="Calculate", command=self.computeResult).grid(row=5, column=2, sticky=E)
window.mainloop()
def computeResult(self):
initRes = self.getResult(float(self.investmentAmount.get()), int(self.years.get()),
float(self.annualInterestRate.get()) / 1200)
self.futureValue.set(format(initRes, "10.2f"))
def getResult(self, investmentAmount, years, annualInterestRate):
res = investmentAmount * (1 + annualInterestRate) ** (years * 12)
return res
InterestCal()