Python使用Tkinter的贷款计算器详细介绍

先决条件:Tkinter介绍
Python提供了多种开发GUI(图形用户界面)的选项。在所有GUI方法中, tkinter是最常用的方法。它是Python随附的Tk GUI工具包的标准Python接口。带有tkinter的Python输出了创建GUI应用程序的最快, 最简单的方法。使用tkinter创建GUI很容易。
创建一个tkinter:

  1. 导入模块– tkinter
  2. 创建主窗口(容器)
  3. 将任意数量的小部件添加到主窗口
  4. 将事件触发器应用于小部件。
让我们看看如何使用Python GUI库Tkiner创建贷款计算器。该计算器将能够根据贷款金额, 期限和利率计算总金额和每月还款额。
第1步:创建一个主窗口。
def __init__( self ): # Create a window window = Tk() window.title( "Loan Calculator" ) # Set title # create the input boxes. Label(window, text = "Annual Interest Rate" ).grid(row = 1 , column = 1 , sticky = W) Label(window, text = "Number of Years" ).grid(row = 2 , column = 1 , sticky = W) Label(window, text = "Loan Amount" ).grid(row = 3 , column = 1 , sticky = W) Label(window, text = "Monthly Payment" ).grid(row = 4 , column = 1 , sticky = W) Label(window, text = "Total Payment" ).grid(row = 5 , column = 1 , sticky = W) # for taking inputs self .annualInterestRateVar = StringVar() Entry(window, textvariable = self .annualInterestRateVar, justify = RIGHT).grid(row = 1 , column = 2 ) self .numberOfYearsVar = StringVar() Entry(window, textvariable = self .numberOfYearsVar, justify = RIGHT).grid(row = 2 , column = 2 ) self .loanAmountVar = StringVar() Entry(window, textvariable = self .loanAmountVar, justify = RIGHT).grid(row = 3 , column = 2 ) self .monthlyPaymentVar = StringVar() lblMonthlyPayment = Label(window, textvariable = self .monthlyPaymentVar).grid(row = 4 , column = 2 , sticky = E) self .totalPaymentVar = StringVar() lblTotalPayment = Label(window, textvariable = self .totalPaymentVar).grid(row = 5 , column = 2 , sticky = E)# create the button btComputePayment = Button(window, text = "Compute Payment" , command = self .computePayment).grid( row = 6 , column = 2 , sticky = E) # Create an event loop window.mainloop()

第2步:
添加功能。
def computePayment( self ): # compute the total payment. monthlyPayment = self .getMonthlyPayment( float ( self .loanAmountVar.get()), float ( self .annualInterestRateVar.get()) /1200 , int ( self .numberOfYearsVar.get())) self .monthlyPaymentVar. set ( format (monthlyPayment, '10.2f' )) totalPayment = float ( self .monthlyPaymentVar.get()) * 12 \ * int ( self .numberOfYearsVar.get()) self .totalPaymentVar. set ( format (totalPayment, '10.2f' )) # compute the monthly payment. def getMonthlyPayment( self , loanAmount, monthlyInterestRate, numberOfYears): monthlyPayment = loanAmount * monthlyInterestRate / ( 1 - 1 /( 1 + monthlyInterestRate) * * (numberOfYears * 12 )) return monthlyPayment;

步骤3:
完成程序
# Import tkinter from tkinter import * class LoanCalculator:def __init__( self ):window = Tk() # Create a window window.title( "Loan Calculator" ) # Set title # create the input boxes. Label(window, text = "Annual Interest Rate" ).grid(row = 1 , column = 1 , sticky = W) Label(window, text = "Number of Years" ).grid(row = 2 , column = 1 , sticky = W) Label(window, text = "Loan Amount" ).grid(row = 3 , column = 1 , sticky = W) Label(window, text = "Monthly Payment" ).grid(row = 4 , column = 1 , sticky = W) Label(window, text = "Total Payment" ).grid(row = 5 , column = 1 , sticky = W)# for taking inputs self .annualInterestRateVar = StringVar() Entry(window, textvariable = self .annualInterestRateVar, justify = RIGHT).grid(row = 1 , column = 2 ) self .numberOfYearsVar = StringVar()Entry(window, textvariable = self .numberOfYearsVar, justify = RIGHT).grid(row = 2 , column = 2 ) self .loanAmountVar = StringVar()Entry(window, textvariable = self .loanAmountVar, justify = RIGHT).grid(row = 3 , column = 2 ) self .monthlyPaymentVar = StringVar() lblMonthlyPayment = Label(window, textvariable = self .monthlyPaymentVar).grid(row = 4 , column = 2 , sticky = E)self .totalPaymentVar = StringVar() lblTotalPayment = Label(window, textvariable = self .totalPaymentVar).grid(row = 5 , column = 2 , sticky = E)# create the button btComputePayment = Button(window, text = "Compute Payment" , command = self .computePayment).grid( row = 6 , column = 2 , sticky = E) window.mainloop() # Create an event loop# compute the total payment. def computePayment( self ):monthlyPayment = self .getMonthlyPayment( float ( self .loanAmountVar.get()), float ( self .annualInterestRateVar.get()) /1200 , int ( self .numberOfYearsVar.get()))self .monthlyPaymentVar. set ( format (monthlyPayment, '10.2f' )) totalPayment = float ( self .monthlyPaymentVar.get()) * 12 \ * int ( self .numberOfYearsVar.get())self .totalPaymentVar. set ( format (totalPayment, '10.2f' ))def getMonthlyPayment( self , loanAmount, monthlyInterestRate, numberOfYears): # compute the monthly payment. monthlyPayment = loanAmount * monthlyInterestRate /( 1 - 1 /( 1 + monthlyInterestRate) * * (numberOfYears * 12 )) return monthlyPayment; root = Tk() # create the widget# call the class to run the program. LoanCalculator()

输出如下:
Python使用Tkinter的贷款计算器详细介绍

文章图片
代码说明:
  • tkinter模块包含tk工具包。在此示例中, 我们在第一行中导入了tkinter的整个模块。接下来, 我们将创建一个名为的用户定义类贷款计算器它拥有自己的数据成员和成员函数。
  • def__init __()是Python类中的一种特殊方法。它是Python类的构造函数, 然后我们使用Tk()创建一个窗口。 label函数创建一个显示框以接受输入, 并使用网格方法为其提供类似表格的结构。
为什么我们使用粘性争论?
默认情况下, 小部件在中心创建, 使用粘性参数可以更改它。它采用4个值:N, S, E, W, 即北, 东, 南, 西。
  • 然后我们创建一些名为self.annualInterestRateVar, self.numberOfYearsVar, self.loanAmountVar, self.monthlyPaymentVar, 自付款总额对于前三个对象, 我们使用条目()功能。
  • 然后我们创建一个按钮, 即计算付款, 当你单击该按钮时, 它将调用属于类别贷款计算器的计算付款功能。使用mainloop函数, 我们运行应用程序。
  • 创建一个功能计算付款()在班里。在这里, 我们将对象的输入存储在一些变量中, 以简化数学计算。
  • 在下一步中, 我们创建另一个名为getMonthlyPayment()在班里。在获得所需的输入后, 它将使用程序中提供的简单数学函数来计算每月付款。
  • 现在根= Tk()初始化的手段Tkinter, 我们必须创建一个窗口小部件。注意, 必须先创建根窗口小部件, 然后再创建其他任何窗口小部件。
【Python使用Tkinter的贷款计算器详细介绍】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读