# Python學習筆記
## tkinkter+function
Tkinter 程式視窗設計的 BMI 計算程式:
$$BMI=\frac{體重(kg)}{身高^2(cm)}$$
```mermaid
graph TD;
體重-->體重過輕;
體重-->健康體位;
體重-->體位異常;
體位異常-->過重;
體位異常-->輕度肥胖;
體位異常-->中度肥胖;
體位異常-->重度肥胖;
```
|成人肥胖定義 | 身體質量指數(BMI)|
| -------- | -------- |
|體重過輕 |$$BMI\lt18.5$$ |
|健康體位 | $$18.5\le BMI \lt24$$ |
|過重 | $$24\le BMI \lt27$$ |
|輕度肥胖 | $$27\le BMI \lt30$$ |
|中度肥胖 | $$30\le BMI \lt35$$ |
|重度肥胖 |$$ BMI \ge35$$ |
## 執行結果:
:::info
$$BMI\lt18.5體重過輕$$
:::

:::success
$$18.5\le BMI \lt24健康體位$$
:::

:::warning
$$24\le BMI \lt27,過重$$
:::

:::danger
$$27\le BMI \lt30,輕度肥胖$$
:::

:::success
$$30\le BMI \lt35,中度肥胖$$
:::

:::info
$$ BMI \ge35,重度肥胖$$
:::

## 心得:
我覺得python的模組非常強大,在學習tkinkter後我學到要如何製作一個計算bmi的程式,透過這個課程讓我能應用tkinkter做其他我想做網頁設計相關的事。
## 程式碼:
```python=
import tkinter as tk
from tkinter import messagebox
root = tk.Tk() # 建立 tkinter 視窗物件
root.title('TKtry')
root.configure(background='#000000') # 設定背景色黑色
width = 200
height = 100
x = 100
y = 200
root.geometry(f'{width}x{height}+{x}+{y}') # 定義視窗的尺寸和位置
root.minsize(1000, 800) # 設定視窗最小為 200x200
root.maxsize(500, 500) # 設定視窗最大為 500x500
root.resizable(False, False) # 設定 x 方向和 y 方向都不能縮放
# tk.Label(root, text='hello world').pack() # 如果不使用變數,也可使用這種做法
root.title('BMI計算')
root.geometry('200x200')
W=tk.StringVar()
H=tk.StringVar()
def BMI():
h=eval(H.get())
w=eval(W.get())
mh=h/100
bmi=w/(mh**2)
if bmi>=35:
health="重度肥胖"
elif bmi>=30:
health="中度肥胖"
elif bmi>=27:
health="輕度肥胖"
elif bmi>=24:
health="過重"
elif bmi>=18.5:
health="健康體位"
else:
health="過輕"
#print("BMI={:.4f} {}".format(bmi,health))
messagebox.showinfo('showinfo', 'BMI={:.2f}{}'.format(bmi,health))
print("BMI={:.4f} {}".format(bmi,health))
#print('BMI={:.2f}'.format(bmi))
mylabel = tk.Label(root,
text='hello world\n大家好',
font=('',20,''),
fg='#000',
bg='#cece60',
pady=30,
padx=40,
relief='groove',
)
lb_height= tk.Label(root,
text='Height',
font=('',20,''),
fg='#000',
bg='#cece60',
pady=20,
padx=20,
relief='groove',
#textvariable=H
)
lb_weight= tk.Label(root,
text='Weight',
font=('',20,''),
fg='#000',
bg='#cece60',
pady=20,
padx=20,
relief='groove',
#textvariable=W
)
mylabel.pack(side='right', padx=20, pady=20,anchor='nw')
#lb_height.pack(side='right', padx=20, pady=20,anchor='nw')
lb_height.place(x='30',y='50')
#lb_weight.pack(side='right', padx=40, pady=20,anchor='nw')
lb_weight.place(x='30',y='200')
mylabel.pack()
btn = tk.Button(root,
text='Count BMI',
font=('',30,''),
padx=10,
pady=10,
activeforeground='#f00',
command=BMI
)
btn.pack()
btn.place(x='300',y='500')
entry_weight = tk.Entry(root,
font=('',30,''),
fg='#000',
bg='#cece60',
textvariable=W
) # 放入單行輸入框
entry_weight.pack()
entry_height = tk.Entry(root,
font=('',30,''),
fg='#000',
bg='#cece60',
textvariable=H
) # 放入單行輸入框) # 放入單行輸入框
entry_height.pack()
entry_height.place(x='300',y='50')
entry_weight.place(x='300',y='200')
root.mainloop() # 放在主迴圈中
```