一、小程序檢測(cè)功能邏輯
即通過(guò)輸入身高、體重兩個(gè)數(shù)據(jù)即可計(jì)算自己的BMI身體指數(shù)是多少,且對(duì)身體狀況做相應(yīng)提醒。
二、小程序使用體驗(yàn)
1,彈出主界面,輸入身高,體重?cái)?shù)據(jù)~

2,點(diǎn)擊計(jì)算,輸出BMI數(shù)據(jù)~

3,根據(jù)BMI結(jié)果,給出相應(yīng)的建議~

三、小程序代碼邏輯
1,引入tkinter庫(kù),構(gòu)建tkinter彈窗框架
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry('350x230+500+230')
root.title('BMI身體指數(shù)計(jì)算器')
root.mainloop()
2,設(shè)置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='計(jì)算', command=jisuan).grid(row=4, column=2, pady=10)
3,編寫(xiě)B(tài)MI計(jì)算邏輯
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身體指數(shù)計(jì)算',
message=f'您的身高為{shengao}m,您的體重為{tizhong}kg,您的BMI身體指數(shù)為{BMI_new}')
if BMI_new < 18.4:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算', message='BMI指數(shù)較低,提示您的身體消瘦,要注意補(bǔ)充營(yíng)養(yǎng)哦!')
elif BMI_new > 18.5 and BMI_new < 24:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算', message='BMI指數(shù)為正常值,繼續(xù)加油!')
elif BMI_new > 24 and BMI_new < 28:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算',
message='BMI指數(shù)較高,屬于是超重了,提示您需要合理飲食,加強(qiáng)鍛煉哦!')
elif BMI_new > 28:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算',
message='BMI指數(shù)很高,屬于肥胖了,提示您需要注意身體健康了,過(guò)胖會(huì)增加人體器官的負(fù)擔(dān)哦!')
4,在tkinter中調(diào)用此函數(shù),即可計(jì)算BMI身體指數(shù)了。
全部代碼:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-607559.html
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry('350x230+500+230') # 設(shè)置彈出框位置和大小
root.iconbitmap('E:/pythonProject/3.ico') # 設(shè)置彈出框圖標(biāo)
root.title('BMI身體指數(shù)計(jì)算器')
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身體指數(shù)計(jì)算',
message=f'您的身高為{shengao}m,您的體重為{tizhong}kg,您的BMI身體指數(shù)為{BMI_new}')
if BMI_new < 18.4:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算', message='BMI指數(shù)較低,提示您的身體消瘦,要注意補(bǔ)充營(yíng)養(yǎng)哦!')
elif BMI_new > 18.5 and BMI_new < 24:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算', message='BMI指數(shù)為正常值,繼續(xù)加油!')
elif BMI_new > 24 and BMI_new < 28:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算',
message='BMI指數(shù)較高,屬于是超重了,提示您需要合理飲食,加強(qiáng)鍛煉哦!')
elif BMI_new > 28:
messagebox.showinfo(title='BMI身體指數(shù)計(jì)算',
message='BMI指數(shù)很高,屬于肥胖了,提示您需要注意身體健康了,過(guò)胖會(huì)增加人體器官的負(fù)擔(dān)哦!')
tk.Button(page, text='計(jì)算', command=jisuan).grid(row=4, column=2, pady=10)
root.mainloop()
文章到此結(jié)束嘍~~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-607559.html
到了這里,關(guān)于python入門(mén)學(xué)習(xí)之小工具制作系列--02使用tkinter庫(kù)寫(xiě)一個(gè)BMI身體指數(shù)檢測(cè)小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!