我编写了这段代码来使用 tkinter 计算 bmi。但如果我运行它,它会给出 float() 参数必须是字符串或数字而不是“nonetype”
我尝试使用 get() 函数
from tkinter import *
import tkinter as tk
from tkinter import ttk
def bmi_cal():
BMI = round(weight / height)
bmi_info = user_name + ",Your BMI is " + str(BMI) + "."
if BMI < 18.5:
print(user_name + ", your BMI is " + str(BMI) + ",you are possibly
underweight or malnourished.")
elif 18.5 < BMI < 24.29:
print(user_name + ", your BMI is " + str(BMI) + ", you are within
healthy range.")
elif 25.0 < BMI < 29.9:
print(user_name + ", your BMI is " + str(BMI) + ", you are
overweight which is unhealthy. \nPlease see a nutritionist.")
else:
print(user_name + ", your BMI is " + str(BMI) + ", you are possibly
obese, please see a nutritionist.")
a= Tk()
n= 'tahoma', 14, 'bold'
Label(a, text='Username', padx=25, font=(n)).grid(row=0, sticky=W)
User_name = Entry(a, width=25 ).grid(row=0,column=1)
Label(a, text='Your_Weight', padx=25, font=(n)).grid(row=1, sticky=W)
weight = ttk.Entry(a, width=25,).grid(row=1,column=1)
Label(a, text='Your_Weight', padx=25, font=(n)).grid(row=2,sticky=W)
height = ttk.Entry(a, width=25).grid(row=2,column=1)
Button(a,text='Calculate', font=(n),command= bmi_cal).grid(row=3, column=1)
a.mainloop()
结果是体重/高度。
最佳答案
您的定义需要包含您希望传递给它的变量:
def bmi_cal(arg1, arg22, ...):
除此之外,您可能希望将 UI 设置放入类中作为最佳实践
编辑:
我现在无法测试这个,因为我评论的机器上没有 tkinter。但我相信它看起来像这样:
from tkinter import *
import tkinter as tk
from tkinter import ttk
class bmi_calculator():
def __init__(self):
pass
def session(self):
n= 'tahoma', 14, 'bold'
Label(a, text='Username', padx=25, font=(n)).grid(row=0, sticky=W)
user_name = Entry(a, width=25 ).grid(row=0,column=1)
Label(a, text='Your_Weight', padx=25, font=(n)).grid(row=1, sticky=W)
weight = ttk.Entry(a, width=25,).grid(row=1,column=1)
Label(a, text='Your_Weight', padx=25, font=(n)).grid(row=2,sticky=W)
height = ttk.Entry(a, width=25).grid(row=2,column=1)
Button(a,text='Calculate', font=(n),command= self.bmi_cal(weight, height, user_name)).grid(row=3, column=1)
a.mainloop()
@staticmethod
def bmi_cal(weight, height, user_name):
BMI = round(weight / height)
bmi_info = user_name + ",Your BMI is " + str(BMI) + "."
if BMI < 18.5:
print(user_name + ", your BMI is " + str(BMI) + ",you are possibly underweight or malnourished.")
elif 18.5 < BMI < 24.29:
print(user_name + ", your BMI is " + str(BMI) + ", you are within healthy range.")
elif 25.0 < BMI < 29.9:
print(user_name + ", your BMI is " + str(BMI) + ", you are overweight which is unhealthy. \nPlease see a nutritionist.")
else:
print(user_name + ", your BMI is " + str(BMI) + ", you are possibly obese, please see a nutritionist.")
window = bmi_calculator()
window.session()
关于python - 参数必须是字符串或数字,而不是 'NoneType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54559668/