【问题标题】:Credit calculator in Python - how to show result in GUIPython 中的信用计算器 - 如何在 GUI 中显示结果
【发布时间】:2018-03-24 23:10:57
【问题描述】:

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

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

【问题讨论】:

  • Canvas 可用于绘图。
  • 更准确地说...我需要通过按“显示”按钮来显示“K”和“Cn”的结果。结果应该在图片上标记的地方可见。
  • 为什么不把它显示为标签?

标签: python tkinter calculator


【解决方案1】:

根据您的代码:

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 测试,结果:

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

【讨论】:

  • 谢谢!我将对此进行分析,并可能尝试使用新功能进行更新。
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2021-10-22
  • 2010-10-21
  • 2014-04-01
相关资源
最近更新 更多