文章目录
Python房贷计算器GUI1.项目简介2.代码3.打包成.exe文件Python房贷计算器GUI
1.项目简介
使用Pycharm写的房贷计算器GUI,计算两种还款方式(等额本息,等额本金)对比,使用工具如下:
Pycharm .3.0
Python中的tkinter库
GUI界面如下:
源代码及生成的.exe文件
2.代码
from tkinter import *class Calculator:def __init__(self):window = Tk() # 建立窗口window.title("房贷计算器") # 命名窗口标题# 定义StringVar对象动态存储输入框的值self.amountVar = StringVar() # 贷款金额self.rateVar = StringVar() # 年化利率self.yearsVar = StringVar() # 贷款年限self.monthsVar = StringVar() # 已还期数self.monthVar = StringVar() # 第i月self.total_1Var = StringVar() # 等额本息总还款额self.totalInterest_1Var = StringVar() # 等额本息总利息self.totalRepayment_1Var = StringVar() # 等额本息累计已还self.remain_1Var = StringVar() # 等额本息待还金额self.monthRepayment_1Var = StringVar() # 等额本息第i月应还self.total_2Var = StringVar() # 等额本金总还款额self.totalInterest_2Var = StringVar() # 等额本金总利息self.totalRepayment_2Var = StringVar() # 等额本金累计已还self.remain_2Var = StringVar() # 等额本金待还金额self.monthRepayment_2Var = StringVar() # 等额本金第i月应还# 设置一些默认值self.monthsVar.set("0")self.monthVar.set('1')# 整体面板,设置总体的边距frame = Frame(window)frame.pack(padx=20, pady=20)"""添加标签.grid(row, column)设置标签位置"""Label(frame, text="贷款金额").grid(row=1, column=1, columnspan=3)Label(frame, text="年化利率").grid(row=2, column=1, columnspan=3)Label(frame, text="贷款年限").grid(row=3, column=1, columnspan=3)Label(frame, text="已还期数").grid(row=4, column=1, columnspan=3)Label(frame, text="总还款额").grid(row=7, column=1, columnspan=3)Label(frame, text="总利息额").grid(row=8, column=1, columnspan=3)Label(frame, text="累计已还").grid(row=9, column=1, columnspan=3)Label(frame, text="剩余待还").grid(row=10, column=1, columnspan=3)Label(frame, text="第").grid(row=11, column=1)Label(frame, text="月应还").grid(row=11, column=3)"""添加输入和输出框textvariable = self.amountVar 由相应的StringVar对象动态保存输入框中的文本justify=RIGHT 输入框中的文本右对齐sticky=E 控件在单元格中右对齐"""Entry(frame, justify=RIGHT, textvariable=self.amountVar).grid(row=1, column=4)Entry(frame, justify=RIGHT, textvariable=self.rateVar).grid(row=2, column=4)Entry(frame, justify=RIGHT, textvariable=self.yearsVar).grid(row=3, column=4)Entry(frame, justify=RIGHT, textvariable=self.monthsVar).grid(row=4, column=4)Entry(frame, width=4, justify=CENTER, textvariable=self.monthVar).grid(row=11, column=2)Label(frame, textvariable=self.total_1Var).grid(row=7, column=4, sticky=E)Label(frame, textvariable=self.total_2Var).grid(row=7, column=6, sticky=E)Label(frame, textvariable=self.totalInterest_1Var).grid(row=8, column=4, sticky=E)Label(frame, textvariable=self.totalInterest_2Var).grid(row=8, column=6, sticky=E)Label(frame, textvariable=self.totalRepayment_1Var).grid(row=9, column=4, sticky=E)Label(frame, textvariable=self.totalRepayment_2Var).grid(row=9, column=6, sticky=E)Label(frame, textvariable=self.remain_1Var).grid(row=10, column=4, sticky=E)Label(frame, textvariable=self.remain_2Var).grid(row=10, column=6, sticky=E)Label(frame, textvariable=self.monthRepayment_1Var).grid(row=11, column=4, sticky=E)Label(frame, textvariable=self.monthRepayment_2Var).grid(row=11, column=6, sticky=E)# 添加Message存储单位Message(frame, text="元").grid(row=1, column=5)Message(frame, text="%").grid(row=2, column=5)Message(frame, text="年").grid(row=3, column=5)Message(frame, text="月").grid(row=4, column=5)Message(frame, text="元").grid(row=7, column=5)Message(frame, text="元").grid(row=7, column=7)Message(frame, text="元").grid(row=8, column=5)Message(frame, text="元").grid(row=8, column=7)Message(frame, text="元").grid(row=9, column=5)Message(frame, text="元").grid(row=9, column=7)Message(frame, text="元").grid(row=10, column=5)Message(frame, text="元").grid(row=10, column=7)Message(frame, text="元").grid(row=11, column=5)Message(frame, text="元").grid(row=11, column=7)# 空Frame以撑开空间Frame(frame, height=10).grid(row=5, column=4, columnspan=7)# 按钮,事件监听函数为calculateButton(frame, width=19, text="等额本息", command=self.calculate_1).grid(row=6, column=4, columnspan=1, pady=0)Button(frame, width=19, text="等额本金", command=self.calculate_2).grid(row=6, column=6, columnspan=1, pady=0)# 消息循环window.mainloop()# 按钮点击监听def calculate_1(self):"""等额本息法计算函数"""# 获取输入的参数amount = eval(self.amountVar.get())rate = eval(self.rateVar.get()) / 100 / 12 # 将年化利率转为月利率,单位为1years = eval(self.yearsVar.get())months = eval(self.monthsVar.get())month = eval(self.monthVar.get())# 计算每月还款monthRepayment = amount * rate * ((1 + rate ) ** (years * 12)) / ((1 + rate ) ** (years * 12) - 1)# 将计算结果设置给控件total = monthRepayment * 12 * years # 总还款金额self.total_1Var.set(format(total, ".3f"))totalInterest = total - amount # 总利息self.totalInterest_1Var.set(format(totalInterest, '.3f'))if (months >= 0) and (months <= years * 12): # 已还期数必须在贷款期数内totalRepayment = monthRepayment * months # 已还金额remain = total - totalRepayment # 剩余待还金额self.totalRepayment_1Var.set(format(totalRepayment, '.3f'))self.remain_1Var.set(format(remain, '.3f'))else:self.totalRepayment_1Var.set('已还期数输入有误!')self.remain_1Var.set('已还期数输入有误!')if (month >= 1) and (month <= years * 12):self.monthRepayment_1Var.set(format(monthRepayment, '.3f'))else:self.monthRepayment_1Var.set('第_月份输入有误!')def calculate_2(self):"""等额本金法计算函数"""# 获取输入的参数amount = eval(self.amountVar.get())rate = eval(self.rateVar.get()) / 100 / 12 # 将年化利率转为月利率,单位为1years = eval(self.yearsVar.get())months = eval(self.monthsVar.get())month = eval(self.monthVar.get())month_amount = amount / (years * 12)# 存储每月还款monthRepayment = []for i in range(1, (years * 12) + 1):monthpayment = month_amount + (amount - (i - 1) * month_amount) * ratemonthRepayment.append(monthpayment)# 将计算结果设置给控件total = sum(monthRepayment)self.total_2Var.set(format(total, ".3f"))totalInterest = total - amountself.totalInterest_2Var.set(format(totalInterest, '.3f'))if (months >= 0) and (months <= years * 12):totalRepayment = sum(monthRepayment[:months])remain = total - totalRepaymentself.totalRepayment_2Var.set(format(totalRepayment, '.3f'))self.remain_2Var.set(format(remain, '.3f'))else:self.totalRepayment_2Var.set('已还期数输入有误!')self.remain_2Var.set('已还期数输入有误!')if (month >= 1) and (month <= years * 12):self.monthRepayment_2Var.set(format(monthRepayment[month - 1], '.3f'))else:self.monthRepayment_2Var.set('第_月份输入有误!')if __name__ == '__main__':Calculator()pass
3.打包成.exe文件
直接在Pycharm运行代码即可,也可以选择打包成电脑软件.exe文件,使用pyinstaller
库。
可以直接pip安装,但可能会遇到安装速度较慢,这里使用清华镜像源,代码如下:
pip install -i https://pypi.tuna./simple pyinstaller
将电脑终端路径切换到Calculator.py文件的目录下,输入:
pyinstaller -F -w Calculator.py
此时,文件夹下面会新增两个文件夹,双击dist文件夹下的Calculator.exe文件即可运行,相应的Calculator.py文件就不需要了!