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

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

结果是体重/高度。

最佳答案

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

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/

相关文章:

python - 清除条目小部件框

c++ - 将 OpenGL 的 glRotated 转换为 Maya 的 cmds.rotate

Python pandas 使用 map 将多列添加到数据框

python - Spyder 中的 "RuntimeError: asyncio.run() cannot be called from a running event loop"

tkinter - 如何获取当前日期以显示在 tkinter 窗口中

python - 添加多个条目并从中动态检索数据

Python pandas 数据透视 matplotlib

python - 如何将插件添加到 Django CMS 中的静态占位符

python - Python3.7 中的触发、遗忘和返回值

python - 当我将存档导入其他代码时,Tkinter Entry 为 0