我試圖在tkinter GUI中使用文本框制作一個貸款攤銷表。然而,所有的貸款攤銷表都作為輸出顯示在終端控制臺。 tkinter文本框的輸出只顯示最后一行。以下是完整的代碼:
import math
import tkinter as tk
from tkinter import *
def calcLoanPayment(p, r, n, t) 。
if p > 0 and r > 0 and n > 0 and t > 0:
clp = (p *(r/n)*(pow(1 r/n, n * t))/(pow(1 r/n, n * t)-1)
clp = round(clp, 2)
return clp
else:
return -1.
def runApp()。#This will bound the function runapp to the calcbtn.
print("running")
p = principalentry.get() #this will get value as a string.
try:
p = float(p) #this will make cast it to float variable..
except:
p = -1.
principalentry.delete(0, END) #this will clear the entry after calculation.
r = interestrateentry.get()
try: #Try將有助于在輸入錯誤值時使程式崩潰。
r = float(r)
except:
r = -1.
interestrateentry.delete(0, END) #這將清除計算后的條目。
n = compoundintervalentry.get() #這將得到一個字串的值。
try:
n = int(n) #this will make cast it to float variable..
except:
n = -1。
compoundintervalentry.delete(0, END) #這將清除計算后的條目。
t = durationentry.get()
try。 #Try will help from crushing the program when wrong value entered。
t = int(t)
except:
t = -1.
durationentry.delete(0, END) #這將清除計算后的條目。
clp = calcLoanPayment(p,r,n,t)
print(clp)
支付間隔 = n*t
Paymentinterval = int(paymentinterval)
起始余額=p
最終余額=p
for i in range(1, paymentinterval 1) 。
利息收費=r/n*起始余額
ENDINGBALANCE=STARTINGBALANCE interestcharge-clp
結果=" " str(i) " " str(round (startingBalance, 1)
) " " str(round (interestcharge, 1)) " " str(round (clp, 1)
) " " str(round (endingBalance, 1)
起點余額=終點余額
print(result)
finaloutput = result
output.config(state="normal")
output.delete(0.0, 'end')
output.insert(END, finaloutput)
output.config(state="disabled")
#Frontend maincode Startshere:
#先勾勒出用戶界面的設計。
root = tk.Tk()
root.config(bg="#F8B135"/span>)
root.state("zoomed")
#There are 3 steps to build event programming[/span].
#Step 1: 構建widgets#step 2: 配置小工具,使其更美觀。
#srep 3: 將小部件放置或打包在根視窗中 "貸款支付計算器"
, font = 'bold')
title.config(fg='white', bg="#28348A")
title.pack(fill = BOTH)
principallabel = Label(root, text="校長:")
principallabel.config(anchor = W, font = 'bold' , bg="#F8B135")
principallabel.place(x=50, y=30)
principalentry = Entry(root)
principalentry.config(bg="#ffffff"/span>)
principalentry.place(x=400, y=30)
interestratelabel = Label(root, text="Interest Rate: enter as Decimal (eg.2% as 0.02)" )
interestratelabel.config(anchor = W, font = 'bold'/span>, bg="#F8B135")
interestratelabel.place(x=50, y=70)
interestrateentry = Entry(root)
interestrateentry.config(bg="#ffffff"/span>)
interestrateentry.place(x=400, y=70)
compoundintervallabel = Label(root, text="compound Interval: ")
compoundintervallabel.config(anchor = W, font = 'bold'/span>, bg="#F8B135")
compoundintervallabel.place(x=50, y=110)
compoundintervalentry = Entry(root)
compoundintervalentry.config(bg="#ffffff"/span>)
compoundintervalentry.place(x=400, y=110)
durationlabel = Label(root, text="Duration: "/span>)
durationlabel.config(anchor = W, font = 'bold', bg="#F8B135")
durationlabel.place(x=50, y=150)
durationentry = Entry(root)
durationentry.config(bg="#ffffff"/span>)
durationentry.place(x=400, y=150)
output = Text(root)
輸出。 config(width=150, height = 100, bg="白色", state="禁用", borderwidth=2, relief="groove", wrap='word')
output.place(x=50, y=230)
calcbtn = Button(root, text= "計算", font = 'bold', highlightbackground="#3E4149")
calcbtn.config(fg='#28348A', command=runApp)
calcbtn.place(x=50, y=190)
root.mainloop()
file.close()`
我不知道如何像終端在tkinter文本框輸出函式中那樣顯示整個輸出。
uj5u.com熱心網友回復:
在函式runApp()
output.config(state="normal"/span>)
# output.delete(0.0, 'end') # 這一行洗掉了文本框中的最后一個條目。洗掉這句話
output.insert(END, finaloutput '
') # 在這里添加一個換行。生成所需的 "表格式 "輸出。
output.config(state="disabled")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/319451.html
標籤:
