一、小程序检测功能逻辑
即通过输入身高、体重两个数据即可计算自己的BMI身体指数是多少,且对身体状况做相应提醒。
二、小程序使用体验
1,弹出主界面,输入身高,体重数据~
![](https://i-blog.csdnimg.cn/blog_migrate/c0aa039b1052a3d058a455e1367e3d3a.png)
2,点击计算,输出BMI数据~
![](https://i-blog.csdnimg.cn/blog_migrate/c8c84663f403e034659e30350cc00319.png)
3,根据BMI结果,给出相应的建议~
![](https://i-blog.csdnimg.cn/blog_migrate/786208607315809107a1388c8a67004f.png)
三、小程序代码逻辑
1,引入tkinter库,构建tkinter弹窗框架
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry('350x230+500+230')
root.title('BMI身体指数计算器')
root.mainloop()
2,设置tkinter弹窗的大小和所处屏幕位置,配置弹窗的输入框,显示文本等信息
height = tk.DoubleVar()
weight = tk.DoubleVar()
page = tk.Frame(root)
page.pack()
tk.Label(page).grid(row=0, column=0)
tk.Label(page, text='身高(米): ').grid(row=2, column=1, pady=20)
tk.Entry(page, textvariable=height).grid(row=2, column=2)
tk.Label(page, text='体重(kg): ').grid(row=3, column=1, pady=20)
tk.Entry(page, textvariable=weight).grid(row=3, column=2)
tk.Button(page, text='计算', command=jisuan).grid(row=4, column=2, pady=10)
3,编写BMI计算逻辑
def jisuan():
shengao = height.get()
tizhong = weight.get()
# print(shengao,tizhong)
if shengao > 0 and tizhong > 0:
BMI = tizhong / shengao ** 2
BMI_new = float(('%.2f' % BMI))
messagebox.showinfo(title='BMI身体指数计算',
message=f'您的身高为{shengao}m,您的体重为{tizhong}kg,您的BMI身体指数为{BMI_new}')
if BMI_new < 18.4:
messagebox.showinfo(title='BMI身体指数计算', message='BMI指数较低,提示您的身体消瘦,要注意补充营养哦!')
elif BMI_new > 18.5 and BMI_new < 24:
messagebox.showinfo(title='BMI身体指数计算', message='BMI指数为正常值,继续加油!')
elif BMI_new > 24 and BMI_new < 28:
messagebox.showinfo(title='BMI身体指数计算',
message='BMI指数较高,属于是超重了,提示您需要合理饮食,加强锻炼哦!')
elif BMI_new > 28:
messagebox.showinfo(title='BMI身体指数计算',
message='BMI指数很高,属于肥胖了,提示您需要注意身体健康了,过胖会增加人体器官的负担哦!')
4,在tkinter中调用此函数,即可计算BMI身体指数了。
全部代码:
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry('350x230+500+230') # 设置弹出框位置和大小
root.iconbitmap('E:/pythonProject/3.ico') # 设置弹出框图标
root.title('BMI身体指数计算器')
height = tk.DoubleVar()
weight = tk.DoubleVar()
page = tk.Frame(root)
page.pack()
tk.Label(page).grid(row=0, column=0)
tk.Label(page, text='身高(米): ').grid(row=2, column=1, pady=20)
tk.Entry(page, textvariable=height).grid(row=2, column=2)
tk.Label(page, text='体重(kg): ').grid(row=3, column=1, pady=20)
tk.Entry(page, textvariable=weight).grid(row=3, column=2)
def jisuan():
shengao = height.get()
tizhong = weight.get()
# print(shengao,tizhong)
if shengao > 0 and tizhong > 0:
BMI = tizhong / shengao ** 2
BMI_new = float(('%.2f' % BMI))
messagebox.showinfo(title='BMI身体指数计算',
message=f'您的身高为{shengao}m,您的体重为{tizhong}kg,您的BMI身体指数为{BMI_new}')
if BMI_new < 18.4:
messagebox.showinfo(title='BMI身体指数计算', message='BMI指数较低,提示您的身体消瘦,要注意补充营养哦!')
elif BMI_new > 18.5 and BMI_new < 24:
messagebox.showinfo(title='BMI身体指数计算', message='BMI指数为正常值,继续加油!')
elif BMI_new > 24 and BMI_new < 28:
messagebox.showinfo(title='BMI身体指数计算',
message='BMI指数较高,属于是超重了,提示您需要合理饮食,加强锻炼哦!')
elif BMI_new > 28:
messagebox.showinfo(title='BMI身体指数计算',
message='BMI指数很高,属于肥胖了,提示您需要注意身体健康了,过胖会增加人体器官的负担哦!')
tk.Button(page, text='计算', command=jisuan).grid(row=4, column=2, pady=10)
root.mainloop()
文章到此结束喽~~