I think I am having a challenge in importing the monthlyPayment and the totalPayment value from the ComputePayment function. So I want to input value in: loanAmount entry box annual interest rate entry box number of years interest box such dat when I press the COMPUTE PAYMENT BUTTON the answers are displayed in the entry of monthly payments and total payments. Looking forwards to your assistance thank you. Below is the code and a picture of what I am trying to run, its a loan calculator:
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
I think that you didn't call the add_frame method so that will be the mistake and i don't know why you have imported pillow ,db, showclient,messagebox. Even without it, it still run.
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()