“无法将字符串转换为浮点数:''

问题描述 投票:0回答:2

我正在尝试计算订单的总和,但 python 一直告诉我它无法将字符串转换为浮点数。怎么解决这个问题?

import tkinter as tk

window = tk.Tk()

window.title("Cinema")
window.geometry("700x600")

def total_():
    a= float(e1.get())*2
    b= float(e2.get())*3
    c= float(e3.get())*4
    d= float(e5.get())*1
    e= float(e4.get())*12
    totl = a+b+c+d+e
    tax= (totl * 0.13 + 0.13)
    lblAns['text'] = "Your total is {}$".format(totl)

#Cinema name
Cine_nom = tk.Label(window)
Cine_nom['text'] = "Welcome to cinema !"
Cine_nom['font'] = "Arial 20 bold"
Cine_nom.place(x=350, y=20, anchor="center")

# Menu Creation
label1 = tk.Label(window,
text="Menu",
font="times 28 bold")

label1.place(x=520, y=70)

label2 = tk.Label(window, text="Popcorn (P) \
2$", font="times 18")

label2.place(x=450, y=120)

label3 = tk.Label(window, text="Popcorn (M) \
3$", font="times 18")

label3.place(x=450, y=150)

label4 = tk.Label(window, text="Popcorn (L) \
4$", font="times 18")
label4.place(x=450, y=180)

label5 = tk.Label(window, text="Burger Combo \
12$", font="times 18")

label5.place(x=450, y=210)

label6 = tk.Label(window, text="Soda \
1$", font="times 18")

label6.place(x=450, y=240)

# Entry Table
label9 = tk.Label(window, text="Select the items",
font="times 18")
label9.place(x=115, y=70)

label10 = tk.Label(window,
text="Popcorn (P)",
font="times 18")
label10.place(x=20, y=120)

e1 = tk.Entry(window)
e1.place(x=20, y=150)

label11 = tk.Label(window, text="Popcorn (M)",
font="times 18")
label11.place(x=20, y=200)

e2= tk.Entry(window)
e2.place(x=20, y=230)

label12 = tk.Label(window, text="Popcorn (L)",
font="times 18")
label12.place(x=20, y=280)

e3= tk.Entry(window)
e3.place(x=20, y=310)

label13 = tk.Label(window,
text="Burger Combo",
font="times 18")
label13.place(x=20, y=360)

e4 = tk.Entry(window)
e4.place(x=20, y=390)

label14 = tk.Label(window,
text="Soda",
font="times 18")
label14.place(x=20, y=420)
e5 = tk.Entry(window)
e5.place(x=20, y=450)

# Button for total
btnCalculate = tk.Button(window)
btnCalculate['text'] = "Calculate your bill"
btnCalculate['font'] = "Arial 15"
btnCalculate['command'] = total_
btnCalculate.place(x=30, y=480)

lblAns = tk.Label(window)
lblAns['text'] = ""
lblAns.place(x=40, y=700)

window.mainloop()
python tkinter type-conversion
2个回答
0
投票

当我向输入框中添加输入时,它对我有用。我认为您只需要移动正在显示的语句即可。我变了

lblAns.place(x=40, y=700)

lblAns.place(x=40, y=550)

然后我在五个输入框中(从上到下)输入 1、2、3、4、5。它打印了

Your total is 73.0$
。鉴于这些是数量,我认为
int
是比
float
更好的选择。

要设置默认值,请为每个条目尝试如下所示的操作。

e1.insert(0, 0)

像下面这样的东西也可以工作。

def total_():
    a= float(e1.get() or 0)*2
    b= float(e2.get() or 0)*3
    c= float(e3.get() or 0)*4
    d= float(e5.get() or 0)*1
    e= float(e4.get() or 0)*12
    totl = a+b+c+d+e
    tax= (totl * 0.13 + 0.13)
    lblAns['text'] = "Your total is {}$".format(totl)

0
投票
# use a debugger or print(type(variable)) to check types
def total_():
    a = float(e1.get() * 2)
    b = float(e2.get() * 3)
    c = float(e3.get() * 4)
    d = float(e5.get() * 1)
    e = float(e4.get() * 12)
    totl = float(a + b + c + d + e)
    tax = float((totl * 0.13) + 0.13)
    lblAns['text'] = f"Your total is {totl}"
© www.soinside.com 2019 - 2024. All rights reserved.