Reputation: 5
Im stuck with showing the results of "Amount of interest for repayment" and "Final Debt Amount" in GUI. "Show" button should draw the result on place marked on picture. Thanks in advance!
from tkinter import *
master = Tk()
master.title("Credit calculator")
Label(master, text="Principal:").grid(row=0)
Label(master, text="Interest rate p(%):").grid(row=1)
Label(master, text="Repayment period in years:").grid(row=2)
Label(master, text="Amount of interest for repayment:").grid(row=3)
Label(master, text="Final Debt Amount:").grid(row=4)
e1 = Entry(master)
C0=e1.grid(row=0, column=1)
e2 = Entry(master)
p=e2.grid(row=1, column=1)
e3 = Entry(master)
n=e3.grid(row=2, column=1)
#Amount of interest for repayment:
# K=(C0*p*n)/100
#Final Debt Amount:
# Cn=C0*(1+(p*n)/100)
Button(master, text='Quit', command=master.quit).grid(row=5, column=0, sticky=E, pady=4)
Button(master, text='Show', command=master.quit).grid(row=5, column=1, sticky=W, pady=4)
mainloop( )
Upvotes: 1
Views: 1665
Reputation: 995
Based on your code:
from tkinter import *
master = Tk()
master.title("Credit calculator")
Label(master, text="Principal:").grid(row=0)
Label(master, text="Interest rate p(%):").grid(row=1)
Label(master, text="Repayment period in years:").grid(row=2)
Label(master, text="Amount of interest for repayment:").grid(row=3)
Label(master, text="Final Debt Amount:").grid(row=4)
e1 = Entry(master)
e1.grid(row=0, column=1)
e2 = Entry(master)
e2.grid(row=1, column=1)
e3 = Entry(master)
e3.grid(row=2, column=1)
K = Entry(master, state=DISABLED)
K.grid(row=3, column=1)
Cn = Entry(master, state=DISABLED)
Cn.grid(row=4, column=1)
def calc(K, Cn):
# get the user input as floats
C0 = float(e1.get())
p = float(e2.get())
n = float(e3.get())
# < put your input validation here >
#Amount of interest for repayment:
K.configure(state=NORMAL) # make the field editable
K.delete(0, 'end') # remove old content
K.insert(0, str((C0 * p * n) / 100)) # write new content
K.configure(state=DISABLED) # make the field read only
#Final Debt Amount:
Cn.configure(state=NORMAL) # make the field editable
Cn.delete(0, 'end') # remove old content
Cn.insert(0, str(C0 * (1 + (p * n) / 100))) # write new content
Cn.configure(state=DISABLED) # make the field read only
Button(master, text='Quit', command=master.quit).grid(row=5, column=0, sticky=E, pady=4)
Button(master, text='Show', command=lambda: calc(K, Cn)).grid(row=5, column=1, sticky=W, pady=4)
mainloop()
Tested on ubuntu 16.04 with Python 3.5.2 with results:
Keep in mind that I don't know a whole lot about economics so I don't know if my testing inputs are good. Still it shouldn't matter in this case.
Upvotes: 0