2017-10-12 53 views
1

Im堅持顯示GUI中「還款利息金額」和「最終債務金額」的結果。 「顯示」按鈕應在圖片上標記的位置繪製結果。提前致謝!Python中的信用計算器 - 如何在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() 
+0

'帆布'可以用於繪圖。 – mentalita

+0

更確切地說...我需要通過按下「顯示」按鈕來顯示「K」和「Cn」的結果。結果應顯示在圖片上標記的位置。 – Prijateljski

+1

爲什麼不把它顯示爲標籤? – mentalita

回答

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

測試在Ubuntu 16.04使用Python 3.5.2與結果:

Credit Calculator

記住我對經濟學並不瞭解,所以我不知道如果我的測試輸入是好的。在這種情況下,這仍然不重要。

+0

謝謝!我會分析這一點,也許嘗試用新功能進行更新。 – Prijateljski