我试图在输入框小部件中显示我的输出,但是执行时什么也没显示,但没有错误

问题描述 投票:1回答:1

我认为从ComputePayment函数导入monthPayment和totalPayment值时遇到了挑战。所以我想输入值:loanAmount输入框年利率输入框年数框这样的数据,当我按下“计算付款”按钮时,答案就会显示在“每月付款”和“总付款”项中。期待您的协助,谢谢。下面是代码和我要运行的图片,它是贷款计算器:

from tkinter import *
from PIL import ImageTk, Image
import db
from tkinter import messagebox
import show_client



class LoanCalculator:
    def __init__(self, data = ''):
        self.data = data
        print(self.data)
        self.win = Tk()
        #Reset The Window & Background Color
        canvas = Canvas(self.win, width=250, height=550, bg = "black")
        canvas.pack(expand=YES, fill=BOTH)

        #Show Window In The Center Of The Screen
        width = self.win.winfo_screenwidth()
        height = self.win.winfo_screenheight()

        x = int(width / 2 - 475 / 2)
        y = int(height / 2 - 400 / 2)

        str1 = "475x400+"+str(x)+"+"+ str(y)
        self.win.geometry(str1)
        self.win.resizable(width=False, height=False)
        #Change Icon
        self.win.iconbitmap("titico.ico")

        self.win.title("LOAN CALCULATOR | AJULES BACAS 1.0")

         #Change Icon
        self.win.iconbitmap("titico.ico")


    def add_frame(self):

        self.frame = Frame(self.win, height=375, width=450)
        self.frame.place(x = 12.5,y=12.5)

        x, y = 70, 20



        self.label = Label(self.frame, text="Please Enter The Loan Details Below:")
        self.label.config(font=('Ebrima', 14, 'bold'))
        self.label.place(x = 60, y = y + 55)

        #Enter Loan Interest Rate
        self.ir = Label(self.frame, text='Loan Annual Interest Rate')
        self.ir.config(font=('Ebrima', 12))
        self.ir.place(x = 5, y = 120)

        self.ir = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.ir.place(x=250, y = 120)

        #Enter Number of Years
        self.noy = Label(self.frame, text='Number of Years')
        self.noy.config(font=('Ebrima', 12))
        self.noy.place(x=5, y = 150)

        self.noy = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.noy.place(x=250, y = 150)

        #Enter Loan Amount
        self.ln = Label(self.frame, text='Loan Amount')
        self.ln.config(font=('Ebrima', 12))
        self.ln.place(x=5, y = 180)

        self.ln = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.ln.place(x=250, y = 180)


        #Monthly Payment
        self.monthlyPayment = Label(self.frame, text='The Monthly Payment Is:')
        self.monthlyPayment.config(font=('Ebrima', 12, 'bold'))
        self.monthlyPayment.place(x=5, y =  290)

        self.monthlyPaymentVar = StringVar()
        self.monthlyPayment = Entry(self.frame, font='Ebrima 12', textvariable = self.monthlyPaymentVar, highlightbackground = "gold", highlightthickness = 1)
        self.monthlyPayment.place(x=250, y = 290)

        #Total Payment
        self.totalPayment = Label(self.frame, text='The Total Payment Is:')
        self.totalPayment.config(font=('Ebrima', 12, 'bold'))
        self.totalPayment.place(x=5, y = 330)

        self.totalPaymentVar = StringVar()
        self.totalPayment = Entry(self.frame, font='Ebrima 12', textvariable=self.totalPaymentVar, highlightbackground = "gold", highlightthickness = 1)
        self.totalPayment.place(x=250, y = 330)

        self.button = Button(self.frame, text='COMPUTE PAYMENT', font='Ebrima 14 bold', fg = "gold", bg = "black", command=self.ComputePayment)
        self.button.place(x = 75, y =  230, width = 300, height = 40)

        return

    def ComputePayment(self):
        self.ir.get()
        self.noy.get()
        self.ln.get()

        ir = float(self.ir.get())
        noy = eval(self.noy.get())
        ln = float(self.ln.get())

        monthlyInterestRate = ir / 1200


        monthlyPayment = ln * monthlyInterestRate / (1 - 1 / (1 + monthlyInterestRate) ** (noy * 12))
        print(monthlyPayment)
        self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))

        totalPayment = monthlyPayment * 12 * noy
        self.totalPaymentVar.set(format(totalPayment, '10.2f'))
        print(totalPayment)
        return 

enter image description here

python-3.x string function tkinter tkinter-entry
1个回答
0
投票

我认为您没有调用add_frame方法,所以这是错误的而且我不知道您为什么要导入枕头,数据库,showclient,消息框。即使没有它,它仍然可以运行。

from tkinter import *
from PIL import ImageTk, Image
import db
from tkinter import messagebox
import show_client



class LoanCalculator:
    def __init__(self, data = ''):
        self.data = data
        print(self.data)
        self.win = Tk()
        #Reset The Window & Background Color
        canvas = Canvas(self.win, width=250, height=550, bg = "black")
        canvas.pack(expand=YES, fill=BOTH)

        #Show Window In The Center Of The Screen
        width = self.win.winfo_screenwidth()
        height = self.win.winfo_screenheight()

        x = int(width / 2 - 475 / 2)
        y = int(height / 2 - 400 / 2)

        str1 = "475x400+"+str(x)+"+"+ str(y)
        self.win.geometry(str1)
        self.win.resizable(width=False, height=False)
        #Change Icon
        self.win.iconbitmap("titico.ico")
        self.add_frame()

        self.win.title("LOAN CALCULATOR | AJULES BACAS 1.0")

         #Change Icon
        self.win.iconbitmap("titico.ico")


    def add_frame(self):

        self.frame = Frame(self.win, height=375, width=450)
        self.frame.place(x = 12.5,y=12.5)

        x, y = 70, 20



        self.label = Label(self.frame, text="Please Enter The Loan Details Below:")
        self.label.config(font=('Ebrima', 14, 'bold'))
        self.label.place(x = 60, y = y + 55)

        #Enter Loan Interest Rate
        self.ir = Label(self.frame, text='Loan Annual Interest Rate')
        self.ir.config(font=('Ebrima', 12))
        self.ir.place(x = 5, y = 120)

        self.ir = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.ir.place(x=250, y = 120)

        #Enter Number of Years
        self.noy = Label(self.frame, text='Number of Years')
        self.noy.config(font=('Ebrima', 12))
        self.noy.place(x=5, y = 150)

        self.noy = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.noy.place(x=250, y = 150)

        #Enter Loan Amount
        self.ln = Label(self.frame, text='Loan Amount')
        self.ln.config(font=('Ebrima', 12))
        self.ln.place(x=5, y = 180)

        self.ln = Entry(self.frame, font='Ebrima 12', highlightbackground = "gold", highlightthickness = 1)
        self.ln.place(x=250, y = 180)


        #Monthly Payment
        self.monthlyPayment = Label(self.frame, text='The Monthly Payment Is:')
        self.monthlyPayment.config(font=('Ebrima', 12, 'bold'))
        self.monthlyPayment.place(x=5, y =  290)

        self.monthlyPaymentVar = StringVar()
        self.monthlyPayment = Entry(self.frame, font='Ebrima 12', textvariable = self.monthlyPaymentVar, highlightbackground = "gold", highlightthickness = 1)
        self.monthlyPayment.place(x=250, y = 290)

        #Total Payment
        self.totalPayment = Label(self.frame, text='The Total Payment Is:')
        self.totalPayment.config(font=('Ebrima', 12, 'bold'))
        self.totalPayment.place(x=5, y = 330)

        self.totalPaymentVar = StringVar()
        self.totalPayment = Entry(self.frame, font='Ebrima 12', textvariable=self.totalPaymentVar, highlightbackground = "gold", highlightthickness = 1)
        self.totalPayment.place(x=250, y = 330)

        self.button = Button(self.frame, text='COMPUTE PAYMENT', font='Ebrima 14 bold', fg = "gold", bg = "black", command=self.ComputePayment)
        self.button.place(x = 75, y =  230, width = 300, height = 40)

        return

    def ComputePayment(self):
        self.ir.get()
        self.noy.get()
        self.ln.get()

        ir = float(self.ir.get())
        noy = eval(self.noy.get())
        ln = float(self.ln.get())

        monthlyInterestRate = ir / 1200


        monthlyPayment = ln * monthlyInterestRate / (1 - 1 / (1 + monthlyInterestRate) ** (noy * 12))
        print(monthlyPayment)
        self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))

        totalPayment = monthlyPayment * 12 * noy
        self.totalPaymentVar.set(format(totalPayment, '10.2f'))
        print(totalPayment)
        return 
LoanCalculator()

gif of your expected output

© www.soinside.com 2019 - 2024. All rights reserved.