用户与另一个小部件交互后,在Python Tkinter Entry小部件中格式化输入的文本

时间:2018-09-12 07:01:32

标签: python-3.x tkinter

如果用户在tkinter条目窗口小部件中输入25000,是否有一种方法可以使该窗口小部件在用户与其他任何内容交互后自动显示刚刚输入的文本,即$ 25,000.00?我在其他项目中使用过'$ {:。,2f}'。format(),但不确定如何在tkinter中使用它。在tkinter中构建代码时,也许完全有另一种方法。预先感谢您可能需要提供的任何建议!

from tkinter import *
from tkinter import messagebox
window=Tk()
window.title("APY Accrued Value Calculator")

def Button1Function():
    exit()

def Button2Function(Entry1,Entry2,Entry3):
    while True:
        if Entry1==0 or Entry2==0 or Entry3==0:
            messagebox.showerror("Error Detected","All fields must have a 
number value greater than zero")
            return None
        if Entry3==int(Entry3):
            Entry3=int(Entry3)
        else:
            messagebox.showerror("Error Detected","Number of years must be 
a whole number.")
            return None
        try:
            if Entry2==int(Entry2):
                Entry2=int(Entry2)
        except: pass
        AccruingAmount=Entry1
        for i in range(0,Entry3):
            AccruingAmount=AccruingAmount*(1+(Entry2/100))
        Entry1='${:,.2f}'.format(Entry1)
        Entry2='{}%'.format(Entry2)
        AccruingAmount='${:,.2f}'.format(AccruingAmount)
        message="                                                                                                                                                              
"
        CalculationMessage=Message(window,width=345,text=message)
        message="If "+Entry1+" is deposited and left for "+str(Entry3)+" 
years at an annual interest rate of "+Entry2+", your final return will be: 
"+AccruingAmount+"."
        CalculationMessage=Message(window,width=345,text=message)
        CalculationMessage.grid(row=12,column=0,sticky=(N),pady=(5,0))
        break

Label1=Label(window,text="Starting amount:")
Label1.grid(column=0,sticky=(W),padx=(5,0))

StrVar1=StringVar()

Entry1=Entry(window,textvariable=StrVar1)
Entry1.grid(column=0,sticky=(W),padx=(5,0))

Label2=Label(window,text="APY percentage rate:")
Label2.grid(row=3,column=0,sticky=(W),padx=(5,0),pady=(10,0))

StrVar2=StringVar()
Entry2=Entry(window, textvariable=StrVar2)
Entry2.grid(column=0,sticky=(W),padx=(5,0))

Label3=Label(window,text="Number of years that starting amount will be 
accruing interest:")
Label3.grid(row=6,column=0,columnspan=2,sticky=(W),padx=(5,0),pady=(10,0))

StrVar3=StringVar()
Entry3=Entry(window, textvariable=StrVar3)
Entry3.grid(row=7,column=0,sticky=(W),padx=(5,0))

BlankRow3=Label(window,text="Calculation results:",font=("bold"))
BlankRow3.grid(row=8,column=0,pady=(10,0))

CalculationBox=Canvas(window,borderwidth=2,width=350,height=25)
CalculationBox.grid(row=12,column=0)

Button1=Button(window,text="Close",width=12,command=Button1Function)
Button1.grid(row=13,column=0,sticky=(W),padx=(5,0),pady=(0,5))

try:
    Button2=Button(window,text="Calculate",width=12,command=lambda: Button2Function(float(Entry1.get()),float(Entry2.get()),float(Entry3.get())))
    Button2.grid(row=13,column=0,sticky=(E),padx=(0,5),pady=(0,5))
except: messagebox.showerror("Error Detected","Numbers must be entered 
into all fields.")

window.mainloop()

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

import tkinter as tk

root = tk.Tk()
amount = tk.Label(root,text = "$0.00")
amount.grid(row=1,column=0)
def updat():
    try:amount.config(text="${:,.2f}".format(float(entry.get())))
    except:amount.config(text = "$0.00")
entry = tk.Entry(root)
entry.grid(row=0,column=0)
entry.bind("<Key>",lambda v: root.after(10,updat))
root.mainloop()

运行:

output