python作业--GUI小作业

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()

效果

在这里插入图片描述

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值