我是初學者。我已經嘗試了一切,以使下面的代碼將數字輸入輸入到輸入框中,並使用它們進行計算。我得到了ValueError,我沒有做任何事情讓這種停止發生。這應該是一個計算每月利息支付和支出總額的計劃。我把它放在一個簡單的產品上,直到我解決這個更基本的問題。謝謝。將tkinter輸入框的值變成浮點數(python)
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()
在哪條線上拋出錯誤? –
第一次演員正在拋出浮動。但我知道了。所以感謝您花時間回覆。我知道我必須努力學習。至少這次我做了它的工作。 – user3527876