使tkinter入口盒值成为一个浮点数(python)

时间:2017-05-06 20:00:06

标签: python tkinter casting

我是初学者。我已经尝试了一切来使下面的代码将数字输入带入输入框并用它们进行计算。我得到了ValueError,而我所做的一切都没有让它停止发生。这应该是一个计算每月利息支付和支付总额的计划。我将它保存在一个简单的产品上,直到我解决了这个更基本的问题。感谢。

def multiply(var1, var2, var3):
    product = float(var1 * var2 * var3)
    return product


def btnClick(event):
    x = float(entry.get())


main = Tk()
main.title("Assignment 16")

main.geometry("500x500")
main["bg"] = "#000066"

lblFirst = Label(main, text="Amount to Pay: ")
lblFirst.grid(row=0, column=3, pady=5)
entry = Entry(main, width=20)
entry.grid(row=0, column=4)
amount = entry.get()
lblSecond = Label(main, text="Interest Rate (like 7.5): ")
lblSecond.grid(row=2, column=3, pady=10)
entry2 = Entry(main, width=20)
entry2.grid(row=2, column=4)
rate = entry2.get()
lblThird = Label(main, text="Years to Pay: ")
lblThird.grid(row=4, column=3, pady=15)
entry3 = Entry(main, width=20)
entry3.grid(row=4, column=4)
years = entry3.get()

try:
    # Try to make it a float
    if amount.isnumeric():
        amount = float(amount)

except ValueError:
    # Print this if the input cannot be made a float
    print("Bad input")

try:
    # Try to make it a float
    if rate.isnumeric():
        rate = float(rate)

except ValueError:
    # Print this if the input cannot be made a float
    print("Bad input")

try:
    # Try to make it a float
    if years.isnumeric():
        years = int(years)

except ValueError:
    # Print this if the input cannot be made a float
    print("Bad input")

lblFourth = Label(main, text="Monthly Payment: ")
lblFourth.grid(row=6, column=3, pady=15)
lblFourthTwo = Label(main, text="XXXXX")
lblFourthTwo.grid(row=6, column=4)
lblFifth = Label(main, text="Total of Paymenta: ")
lblFifth.grid(row=8, column=3, pady=15)
lblFifthTwo = Label(main, text="XXXXX")
lblFifthTwo.grid(row=8, column=4)

button1 = Button(main, text='Convert', width=10, command=btnClick)
button2 = Button(main, text='Calculate', width=10, command=multiply(amount, rate, years))
button1.grid(padx=20, pady=20)

main.mainloop()

0 个答案:

没有答案