Python中的信用计算器 - 如何在GUI中显示结果

时间:2017-10-12 21:44:01

标签: python tkinter calculator

我坚持要显示"还款利息金额的结果"和#34;最终债务金额"在GUI中。 "显示"按钮应该在图片上标记的位置绘制结果。提前谢谢!

GUI example

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( )

1 个答案:

答案 0 :(得分:0)

根据您的代码:

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()

使用Python 3.5.2在ubuntu 16.04上测试结果:

Credit Calculator

请记住,我对经济学知之甚少,所以我不知道我的测试输入是否良好。在这种情况下,它仍然无关紧要。