# -*- coding: utf-8 -*- import tkinter as tk root = tk.Tk() root.title("tk12.py") root.geometry("300x200") ##################################### def BMI(event): w = float(eWeight.get()) h = float(eHeight.get()) bmi = w/h/h if bmi < 18.5: bmitext = '太輕了,要多吃一點' lbBMI.config(fg = 'magenta', bg = 'lightgreen') elif 18.5<=bmi<23.9: bmitext = '標準身材,請好好保持' lbBMI.config(fg = 'blue', bg = 'lightpink', font = 'Harrington 12') elif 23.9<=bmi<27.9: bmitext = '喔喔!得控制一下飲食了\n請加油!' lbBMI.config(fg = 'red', bg = 'gold', font = '標楷體 14 italic') else: bmitext = '肥胖容易引起疾病\n得要多多注意自己的健康囉!' lbBMI.config(fg = 'white', bg = 'black', font = 'Times 16 bold') lbBMI.config(text = 'Your BMI = {}\n{}'.format(bmi,bmitext)) lbWeight = tk.Label(root, text = '請輸入體重(公斤)'\ , font="標楷體") eWeight = tk.Entry(root, bg = 'lightgreen',\ bd = 2, font = "標楷體", width = 50) lbHeight = tk.Label(root, text = '請輸入身高(公尺)'\ , font="標楷體") eHeight = tk.Entry(root, bg = 'lightgreen',\ bd = 2, font = "標楷體", width = 50) btBMI = tk.Button(root, text = 'BMI') lbBMI = tk.Label(root) lbWeight.pack() eWeight.pack() lbHeight.pack() eHeight.pack() btBMI.pack() lbBMI.pack() btBMI.bind('<1>', BMI) ##################################### root.mainloop()