#python ##tkingter+funtion $$BMI=\frac{體重(kg)}{身高(m)^2}$$ ## 執行結果 |成人|身體指數BMI| |-------|------| |體重過輕|BMI<18.5| |體重標準|18.5<=BMI<24| |體重過重|24<=BMI<27| |輕度肥胖|27<=BMI<30| |中度肥胖|30<=BMI<35| |重度肥胖|BMI>=35| ```mermaid graph TD; 求出BMI-->體重過輕; 求出BMI-->健康體位; 求出BMI-->體位異常; 體位異常-->過重; 體位異常-->輕度肥胖; 體位異常-->中度肥胖; 體位異常-->重度肥胖; ``` ## 執行結果 :::info $$體重過輕→BMI\lt24$$ ![](https://i.imgur.com/W8xhBLF.png) ::: :::warning $$標準體位→18.5\le BMI\lt24$$ ![](https://i.imgur.com/7kzoTbm.png) ::: :::warning $$過重→24\le BMI\lt27$$ ![](https://i.imgur.com/hJOEEbG.png) ::: :::warning $$輕度肥胖→27\le BMI\lt30$$ ![](https://i.imgur.com/vV7rDfs.png) ::: :::danger $$中度肥胖→30\le BMI\lt35$$ ![](https://i.imgur.com/EgJLPVn.png) ::: :::danger $$重度肥胖→ BMI\ge35$$ ![](https://i.imgur.com/E9LWL7E.png) ::: ## 心得感想 很高興這次的加深加廣選擇了這堂課,認識了TKinter語法能夠做出一個算BMI的網頁讓我感到新奇,也很有成就感,雖然以前也打過BMI的程式,但這是我第一次將它做成網頁形式非常的好玩。我們還學習了用HackMD做程式筆記,沒想到竟然可以用程式碼做出表格和流程圖,使我收穫良多,也許下次在製作簡報流程圖時我可以試著自己用HackMD做,謝謝老師的指導。 ## 程式碼 ```python= import tkinter as tk from tkinter import messagebox root = tk.Tk() # 建立 tkinter 視窗物件 root.title('MDFC') root.configure(background='#f7e192') # 設定背景色黑色 width = 400 height = 400 x = 300 y = 300 root.geometry(f'{width}x{height}+{x}+{y}') # 定義視窗的尺寸和位置 root.minsize(100,100) # 設定視窗最小為 200x200 root.maxsize(980, 1000) # 設定視窗最大為 500x500 root.resizable(False, True) # 設定 x 方向和 y 方向都不能縮放 H=tk.StringVar() W=tk.StringVar() #BMI計算因式 def BMI(): h=eval(H.get()) w=eval(W.get()) h=h/100 bmi=w/h**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={},{}'.format(bmi,health)) messagebox.showinfo('你的BMI值為', 'BMI={:.4f} {}'.format(bmi,health)) #身高 body_height = tk.Label(root, text='身高', font=('',20,''), fg='#f5edef', bg='#801f38', relief='raised', pady=1, padx=10) #body_height.pack(side='left', padx=10, pady=10,anchor='nw') body_height.place(x=20,y=40) #體重 body_weight = tk.Label(root, text='體重', font=('',20,''), fg='#f5edef', bg='#801f38', relief='raised', pady=1, padx=10) #body_weight.pack(side='left', padx=10, pady=10,anchor='nw') body_weight.place(x=20,y=120) btn = tk.Button(root, text='BMI計算', fg='#0f0d0f', bg='#e7c7ed', font=('',20,''),padx=10,pady=5, command=BMI ) # 建立 Button 按鈕 btn.place(x=10,y=200) entry_height = tk.Entry(root, font=('',20,''), fg='#4fb0e8', bg='#262220', textvariable=H ) # 放入單行輸入框 entry_height.place(x=100,y=40) entry_weight = tk.Entry(root, fg='#f5d556', bg='#262220', font=('',20,''), textvariable=W) # 放入單行輸入框 entry_weight.place(x=100,y=120) root.mainloop() # 放在主迴圈中