我搞不懂我的Tkinter按揭计算器怎么了?任何建议都是错误的

2024-04-27 09:03:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对此代码有问题。要计算你每月要付多少房贷,尽管我和每个人都谈过,但都不能确定问题出在哪里。所以我来这里是想看看你们谁能帮忙?非常感谢:)

from tkinter import * #Imports the tkinter library
mortgage=Tk() #Names the interface as 'mortgage'
mortgage.geometry("800x500+200+200")  #Defines the dimensions of the interface
mortgage.title("Mortgage")

#calculation method
def Calculate():
    loantotal1=float(loantotal.get())
    interestrate1=float(interestrate.get())
    loanperiod1=float(loanperiod.get())

    interestCalculation =float( interestrate1 / 100)
    totalcost = float(loantotal1 * interestCalculation)
    numberofmonths=float(loanperiod1 / 12)
    monthlycost= float(totalcost / loanperiod1)
    monthly_period.set=("Your monthly payment %2.f "%monthlycost)

#variables
loantotal=StringVar()
interestrate=StringVar()
loanperiod=StringVar()
monthly_period=StringVar()

#interface
welcome_message=Label(mortgage,text="Find out how much you'll pay monthly    for your mortgage,",font="Arial 12 bold",fg="#000000").pack()
instruction_message=Label(mortgage,text="Enter your details below and then          Click submit:",font="Arial 12 bold",fg="#000000").pack()

loanTotal=Label(mortgage,text="Loan Total:",font="Arial 11     bold",fg="#000000").place(x=280,y=63)
loanTotal=Label(mortgage,text="£",font="Arial 11 bold",fg="#000000").place(x=550,y=63)
loanTotal_textbox=Entry(mortgage,textvariable=loantotal).place(x=400,y=65)

interestRate=Label(mortgage,text="Interest Rate:",font="Arial 11 bold",fg="#000000").place(x=280,y=88)
interestRate=Label(mortgage,text="%",font="Arial 11 bold",fg="#000000").place(x=550,y=88)
interestRate_textbox=Entry(mortgage,textvariable=interestrate).place(x=400,y=90)

loanPeriod=Label(mortgage,text="Loan Period:",font="Arial 11 bold",fg="#000000").place(x=280,y=113)
loanPeriod=Label(mortgage,text="Years",font="Arial 11 bold",fg="#000000").place(x=550,y=113)
loanPeriod_textbox=Entry(mortgage,textvariable=loanperiod).place(x=400,y=115)

Label(mortgage,textvariable=monthly_period).place(x=400,y=135)



submit_button=Button(mortgage,text="Calculate",command=Calculate,font="Arial 11 bold",fg="#000000").place(x=280,y=170)

mortgage.mainloop()   

Tags: thetextplacefloatlabelinterfacefontcalculate
2条回答
monthly_period.set=("Your monthly payment %2.f "%monthlycost)

您应该调用set方法,而不是赋值给它。。。去掉等号。你知道吗

在代码开头添加:

# -*- coding: utf-8 -*-

另外,如果您使用的是Python2,那么可以这样编写Tk导入:

from Tkinter import * #Imports the tkinter library

相关问题 更多 >