python入门学习之小工具制作系列--02使用tkinter库写一个BMI身体指数检测小程序

一、小程序检测功能逻辑

即通过输入身高、体重两个数据即可计算自己的BMI身体指数是多少,且对身体状况做相应提醒。

二、小程序使用体验

1,弹出主界面,输入身高,体重数据~

2,点击计算,输出BMI数据~

3,根据BMI结果,给出相应的建议~

三、小程序代码逻辑

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

文章到此结束喽~~

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值