当前位置:首页>python>python高阶:31、体脂率计算器

python高阶:31、体脂率计算器

  • 2026-10-26 14:47:54
python高阶:31、体脂率计算器
1、使用说明:

(1)依次输入体重(kg)、身高(cm) 和年龄(整数)。

(2)通过单选按钮选择性别(男/女)。

(3)点击 “计算体脂率” 按钮,系统将弹出结果对话框,显示您的体脂率百分比。

(4)公式基于BMI与年龄、性别修正,结果仅供参考。请确保输入有效数字,否则程序可能报错。

import tkinter as tkfrom tkinter import messageboxroot=tk.Tk()   #创建主窗口的实例对象rootroot.title("体脂率计算器")root.geometry("300x450+600+200")root.resizable(False,False)sex_var=tk.IntVar()def calculator():   #获取数据,根据公式计算体脂率    w=int(weight.get())   #体重,单位kg    h=int(height.get())   #身高,单位cm    s=sex_var.get()          #性别编号:男1,女0    a=int(age.get())      #年龄    if s==1:  #男性计算公式        bmi=w/((h/100)**2)        body_fat=1.2*bmi+0.23*a-5.4-10.8*s        messagebox.showinfo("男性体脂率","你的体脂率是{:0.2%}".format(body_fat/100))    else:        bmi=w/((h/100)**2)        body_fat=1.2*bmi+0.23*a-5.4-10.8*s        messagebox.showinfo("女性体脂率","你的体脂率是{:0.2%}".format(body_fat/100))#标题label=tk.Label(root,text="体脂率计算器",font=("黑体",25,"normal"))label.pack(pady=5)#输入体重weight_label=tk.Label(root,text="请输入体重(kg):",font=("黑体",15,"normal"))weight_label.pack(pady=5)weight=tk.Entry(root,width=20)weight.pack(pady=5)#输入身高height_label=tk.Label(root,text="请输入身高(cm):",font=("黑体",15,"normal"))height_label.pack(pady=5)height=tk.Entry(root,width=20)height.pack(pady=5)#选择性别sex_label=tk.Label(root,text="请选择性别:")sex_label.pack(pady=1)radio_man=tk.Radiobutton(root,text="男",value=1,variable=sex_var)radio_man.pack(pady=1)radio_woman=tk.Radiobutton(root,text="女",value=0,variable=sex_var)radio_woman.pack(pady=1)#输入年龄age_label=tk.Label(root,text="请输入年龄:",font=("黑体",15,"normal"))age_label.pack(pady=5)age=tk.Entry(root,width=20)age.pack(pady=5)#按钮bt=tk.Button(root,text="计算体脂率",command=calculator)bt.pack(pady=5)tk.mainloop()

最新文章

随机文章