ValueError:无法将字符串转换为float:在tkinter

时间:2017-11-23 17:23:53

标签: python string tkinter floating-point tkinter-entry

我无法将条目小部件值转换为float以进行函数内的计算。

from tkinter import *

def bac_calc():

    #gender_condition
    gend=gender.get()
    if gend == "Male":
        k1=0.015
        k2=0.080
    elif gend == "Female":
        k1=0.020
        k2=0.075
    #weight
    kg=body_weight.get()
    kg=float(kg)
    wt_lbs=kg*2.20462

    bac=k1*k2*wt_lbs*100

    t= "weight in pound is " , float(bac) 
    Label(master, text=t).grid(row=23, column=1)


master = Tk()
master.title('Blood Alcohol Level calculator')


#Gender
gender=StringVar()
Label(master, text="Select your gender").grid(row=2, column=0, sticky=W, pady=5)
r1=Radiobutton(master, text="Male", padx=20, variable=gender, value="Male", command=bac_calc).grid(row=3, column=2, sticky=W, pady=5)#anchor
r2=Radiobutton(master, text="Female", padx=20, variable=gender, value="Female", command=bac_calc).grid(row=3, column=3, sticky=W, pady=5)

#Body weight
Label(master, text="Enter the body weight kg").grid(row=5, column=0, sticky=W, pady=5)
body_weight=Entry(master)
body_weight.grid(row=6, column=2)

#Evaluate and Quit
Button(master, text="Evaluate", command=bac_calc).grid(row=8, column=2, sticky=W, pady=5)
Button(master, text="Quit ", command=master.quit).grid(row=9, column=4, sticky=W, pady=5)

master.mainloop()

你能帮我解决这个问题吗?

提前致谢!!

很抱歉代码很长。无论如何,只有输入小部件和计算。

2 个答案:

答案 0 :(得分:0)

根据@PM 2Ring的建议,您应该考虑为Entry所代表的body_weight元素添加默认值。创建insert后,您可以使用body_weight方法执行此操作。

请参阅here

答案 1 :(得分:0)

我已经清理了一点代码,并为浮点转换问题添加了错误处理语句。您可能会考虑做的一件事是找到BAC计算的公式,该计算利用千克而不是将其转换为磅。我也建议查找PEP8 style guides。它有助于保持代码可读性。希望这会有所帮助:)

from tkinter import *


def bac_calc():

    # gender_condition
    gend = gender.get()
    if gend == "Male":
        k1 = 0.015
        k2 = 0.080
    else:
        k1 = 0.020
        k2 = 0.075

    # weight
    try:
        kg = body_weight.get()
        kg = float(kg)
        wt_lbs = kg * 2.20462

        bac = k1 * k2 * wt_lbs * 100

        t = "weight in pound is ", float(bac)
        Label(master, text=t).grid(row=23, column=1)

    except ValueError as e:
        Label(master, text='Input must be numerical.').grid(row=23, column=1)


master = Tk()
master.title('Blood Alcohol Level calculator')

# Gender
gender = StringVar()
Label(master, text="Select your gender").grid(row=2, column=0, sticky=W, pady=5)
r1 = Radiobutton(master, text="Male", padx=20, variable=gender, value="Male", command=bac_calc).grid(row=3, column=2,
                                                                                                     sticky=W,
                                                                                                     pady=5)  # anchor
r2 = Radiobutton(master, text="Female", padx=20, variable=gender, value="Female", command=bac_calc).grid(row=3,
                                                                                                         column=3,
                                                                                                         sticky=W,
                                                                                                         pady=5)

# Body weight
Label(master, text="Enter the body weight kg").grid(row=5, column=0, sticky=W, pady=5)
body_weight = Entry(master)
body_weight.grid(row=6, column=2)

# Evaluate and Quit
Button(master, text="Evaluate", command=bac_calc).grid(row=8, column=2, sticky=W, pady=5)
Button(master, text="Quit ", command=master.quit).grid(row=9, column=4, sticky=W, pady=5)

master.mainloop()