参数必须是字符串或数字,而不是'NoneType'

时间:2019-02-06 17:48:02

标签: python python-3.7 tkinter-entry

我编写了这段代码,以使用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()

结果是重量/高度。

1 个答案:

答案 0 :(得分:0)

您的定义需要包含您希望传递给它的变量:

def bmi_cal(arg1, arg22, ...):

此外,您可能希望将UI设置作为最佳实践放在一个类中

编辑:

我现在无法对此进行测试,因为我没有从机器上收到任何评论。但是我相信它看起来像这样:

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()