python入门学习之小工具制作系列--02使用tkinter库写一个BMI身体指数检测小程序
生活随笔
收集整理的這篇文章主要介紹了
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 ** 2BMI_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 messageboxroot = 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 ** 2BMI_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()文章到此結束嘍~~
總結
以上是生活随笔為你收集整理的python入门学习之小工具制作系列--02使用tkinter库写一个BMI身体指数检测小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关ISP许可证以及ISP停办的原因
- 下一篇: python安装插件失败_cordova