Recently, I often eat late at night because I go to a part-time job or a driving school. So, I thought I was fat, and I wanted to know what kind of system I am now, so I made a tool to measure BMI.
qiita.py
root=tk.Tk()
root.geometry("400x300")
root.title("BMI diagnostic tool")
Prepare height and weight labels, height, weight, BMI, system text boxes, and buttons. Then place them in the right places.
qiita.py
#Make a label
height_lavel=tk.Label(text="height(m)")
height_lavel.place(x=60,y=50)
weight_lavel=tk.Label(text="body weight(kg)")
weight_lavel.place(x=60,y=80)
bmi_lavel=tk.Label(text="BMI")
bmi_lavel.place(x=60,y=200)
result_lavel=tk.Label(text="What is your system?")
result_lavel.place(x=50,y=240)
#Make a text box
height_box=tk.Entry(width=20)
height_box.place(x=140,y=50)
weight_box=tk.Entry(width=20)
weight_box.place(x=140,y=80)
bmi_box=tk.Entry(width=20)
bmi_box.place(x=140,y=200)
result_box=tk.Entry(width=20)
result_box.place(x=140,y=240)
#Make a button
buttonl=tk.Button(root,text="Diagnose",font=("Halvetica",14),command=Buttonclick)
buttonl.place(x=140,y=130)
Since BMI is weight (kg) / height (m) x height (m), apply this formula to output BMI. In addition, this time, when the BMI is below 18.5, the comment "Burning type", between 18.5 and 25, "Standard body type", and when the BMI is 25 or more, "Obesity" is also output.
qiita.py
height=float(height_box.get())
weight=float(weight_box.get())
bmi=weight/(height*height)
if bmi<18.5:
result = "Skinny type"
elif 18.5<=bmi<25:
result = "Standard body type"
elif 25<=bmi:
result = "obesity"
result_box.delete(0,tk.END)
result_box.insert(0,result)
qiita.py
def Buttonclick():
I was able to output safely.
qiita.py
#codimg:utf-8
import tkinter as tk
def Buttonclick():
height=float(height_box.get())
weight=float(weight_box.get())
bmi=weight/(height*height)
bmi_box.delete(0,tk.END)
bmi_box.insert(0,bmi)
if bmi<18.5:
result = "Skinny type"
elif 18.5<=bmi<25:
result = "Standard body type"
elif 25<=bmi:
result = "obesity"
result_box.delete(0,tk.END)
result_box.insert(0,result)
#Make a window
root=tk.Tk()
root.geometry("400x300")
root.title("BMI diagnostic tool")
#Make a label
height_lavel=tk.Label(text="height(m)")
height_lavel.place(x=60,y=50)
weight_lavel=tk.Label(text="body weight(kg)")
weight_lavel.place(x=60,y=80)
bmi_lavel=tk.Label(text="BMI")
bmi_lavel.place(x=60,y=200)
result_lavel=tk.Label(text="What is your system?")
result_lavel.place(x=50,y=240)
#Make a text box
height_box=tk.Entry(width=20)
height_box.place(x=140,y=50)
weight_box=tk.Entry(width=20)
weight_box.place(x=140,y=80)
bmi_box=tk.Entry(width=20)
bmi_box.place(x=140,y=200)
result_box=tk.Entry(width=20)
result_box.place(x=140,y=240)
#Make a button
buttonl=tk.Button(root,text="Diagnose",font=("Halvetica",14),command=Buttonclick)
buttonl.place(x=140,y=130)
root.mainloop()
For the first time, I was able to make something that works by myself with python. It took a while, but I was very happy when it was completed. Also, I think I'll try to make something like this with python even during the summer vacation.
―― "The easiest Python introductory class" Fumitaka Osawa [Author]
-[Python] Sample program for calculating BMI with GUI with Tkinter
Recommended Posts