我试图获取最后一个输入框,使结果以$ X,XXX.XX格式显示,但它始终以标准十进制形式出现,我不知道为什么。
我搜索了一种货币转换功能,发现了“语言环境”,但它似乎没有做任何事情,内置的舍入功能也没有。
from tkinter import *
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
window = Tk()
label1 = Label(window, text = "Invest $10,000")
label1.grid(pady = 5, padx = 5, row = 0, column = 1, sticky = NSEW)
label2 = Label(window, text = "Interest\nrate:")
label2.grid(pady = 5, padx = 5, row = 1, column = 0, sticky = NSEW)
label3 = Label(window, text = "Compound\nperiods:")
label3.grid(pady = 5, padx = 5, row = 1, column = 1, sticky = NSEW)
label4 = Label(window, text = "Amount after 5 years:")
label4.grid(pady = 5, padx = 5, row = 3, column = 0, columnspan = 2, sticky = NSEW)
intrates = ["2%", "2.5%", "3%", "3.5%", "4%"]
ConOFintrate = StringVar()
Listbox1 = Listbox(window, width = 5, height = 5, listvariable = ConOFintrate, exportselection = 0)
Listbox1.grid(padx = 5, pady = 5, row = 2, column = 0, sticky = NSEW)
ConOFintrate.set(tuple(intrates))
ams = ["annually", "semi-annually", "quarterly", "monthly", "weekly"]
ConOFam = StringVar()
Listbox2 = Listbox(window, width = 15, height = 5, listvariable = ConOFam, exportselection = 0)
Listbox2.grid(padx = 5, pady = 5, row = 2, column = 1, sticky = NSEW)
ConOFam.set(tuple(ams))
def calc():
ratedic = {
"2%": .02, "2.5%": .025, "3%": .03, "3.5%": .035, "4%": .04,
}
amdic = {
"annually": 1, "semi-annually": 2, "quarterly": 4, "monthly": 12, "weekly": 52,
}
rateselect = Listbox1.get(Listbox1.curselection())
r = ratedic.get(rateselect)
amselect = Listbox2.get(Listbox2.curselection())
n = amdic.get(amselect)
result = 10000*(1+r/n)**(5*n)
round(result, 2)
locale.currency(result, grouping = True)
finalresult.set(result)
btncalc = Button(window, text = "Calculate\nAmount\nAfter 5\nYears", width = 11, height = 4, command = calc)
btncalc.grid(padx = 5, pady = 5, row = 2, column = 2, sticky = W)
finalresult = StringVar()
resultentry = Entry(window, state = "readonly", textvariable = finalresult, width = 13)
resultentry.grid(padx = 5, pady = 5, row = 3, column = 2, sticky = W)
window.mainloop()
我唯一想念的就是最终结果的货币换算