【发布时间】:2019-01-06 20:40:36
【问题描述】:
最近我一直在编写代码,并在此错误上卡住了好几天。 基本上,该程序会计算您每天必须吃多少卡路里。我必须从一个条目中获取输入,但我不知道如何将该输入(默认为字符串)转换为浮点数以开始使用数字。 我正在使用 Python 3 和 Tkinter。
代码如下:
from tkinter import *
root = Tk()
root.geometry("1000x500")
root.resizable(FALSE, FALSE)
root.title("BMI Calculator")
def calc(args):
def BMI_temp(args):
print(str(boyage))
BMI = IntVar()
BMI = 66.5 + (13.75 * float(boykg)) + (5.003 * float(boycm)) - (6.755 * float(boyage))
bmi_temp = Label(root, text="This is how many calories you have to eat if you have a non-active life: " + str(float(BMI)))
bmi_temp.grid(row=3, sticky=W)
def boy_age_fnct(args):
boy_age_entry.focus_set()
boy_cm_entry.delete(0, "end")
boy_age.grid(row=2, sticky=W)
boy_age_entry.grid(row=2, column=1)
boy_age_entry.bind("<Return>", BMI_temp)
def boy_cm_fnct(args):
boy_cm_entry.focus_set()
boy_kg_entry.delete(0, "end")
boy_cm.grid(row=1, sticky=W)
boy_cm_entry.grid(row=1, column=1)
boy_cm_entry.bind("<Return>", boy_age_fnct)
boy_kg_entry.focus_set()
temp = boygirle.get()
gender = temp.title()
welcome.destroy()
hello_lbl.destroy()
boygirle.destroy()
boygirlq.destroy()
if gender[0] == 'B':
boy_kg.grid(row=0, sticky=W)
boy_kg_entry.grid(row=0, column=1)
boy_kg_entry.bind("<Return>", boy_cm_fnct)
boyage = boy_age_entry.get()
boycm = boy_cm_entry.get()
boykg = boy_kg_entry.get()
def hello(args):
name_user = name_entry.get()
name2 = name_user.title()
name_entry.delete(0, "end")
hello = "Hello " + name2 + "!"
hello_lbl["text"] = hello
hello_lbl.grid(row=2, sticky=W)
btn_cont.grid(row=3, sticky=W)
name.destroy()
name_entry.destroy()
btn_cont.focus_set()
def BMI():
btn_cont.destroy()
boygirlq.grid(row=3, sticky=W)
boygirle.grid(row=3, column=0, ipadx=35)
boygirle.bind("<Return>", calc)
boygirle.focus_set()
welcome = Label(root, text="Hello! This is a BMR calculator. It tells you how many calories you have to eat!", font="System 14 bold")
name = Label(root, text="Please enter your name:", font="System 12")
hello_lbl = Label(root, font="System 14")
boygirlq = Label(root, text="Are you a boy or a girl?", font="System 12 bold")
boy_kg = Label(root, text="Please enter your weight(in kg):", font="System 12 bold")
boy_cm = Label(root, text="Please enter your height(in cm):", font="System 12")
boy_age = Label(root, text="Please enter your age(in years):", font="System 12")
btn_cont = Button(root, text="Continue", font="Helvetica 12", command=BMI, relief=RAISED)
boy_kg_entry = Entry(root, font="System 12", relief=SUNKEN)
boy_cm_entry = Entry(root, font="System 12", relief=SUNKEN)
boy_age_entry = Entry(root, font="System 12", relief=SUNKEN)
name_entry = Entry(root, font="System 12", relief=SUNKEN)
boygirle = Entry(root, font="System 12", relief=SUNKEN)
name_entry.bind("<Return>", hello)
name_entry.focus_set()
welcome.grid(row=0, columnspan=2, ipadx=200)
name.grid(row=1, sticky=W)
name_entry.grid(row=1, column=0)
root.mainloop()
我尝试了我在互联网上找到的所有方法,但没有任何效果。
【问题讨论】:
-
该错误是由对无法转换为浮点数的值调用
float()引起的。如果您仔细查看错误消息,它应该会显示错误的值,例如ValueError: could not convert string to float: apple。如果它只是以float:结尾,则表示输入为空白。 -
我明白,我知道。我想我应该把
boyageboycm和boykg放在其他地方,但在哪里。我知道错误是因为输入未完成。我该怎么办? -
您的
calc()函数应该事先专门检查每个值,如果任何值不能转换为浮点数,则退出。 -
好的,谢谢,我会努力解决的
-
如答案中所述,
ValueError: could not convert string to float从csv文件读取数据帧并转换类型df = df[['p']].astype({'p': float})时可能发生。如果 csv 是用空格记录的,python 将无法识别空格字符。你需要用NaN覆盖空单元格df = df.replace(r'^\s*$', np.nan, regex=True)
标签: python python-3.x tkinter