|
| 1 | +from tkinter import * |
| 2 | +from tkinter import messagebox |
| 3 | + |
| 4 | +def reset_entry(): |
| 5 | + age_tf.delete(0,'end') |
| 6 | + height_tf.delete(0,'end') |
| 7 | + weight_tf.delete(0,'end') |
| 8 | + |
| 9 | +def calculate_bmi(): |
| 10 | + kg = int(weight_tf.get()) |
| 11 | + m = int(height_tf.get())/100 |
| 12 | + bmi = kg/(m*m) |
| 13 | + bmi = round(bmi, 1) |
| 14 | + bmi_index(bmi) |
| 15 | + |
| 16 | +def bmi_index(bmi): |
| 17 | + |
| 18 | + if bmi < 18.5: |
| 19 | + messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Underweight') |
| 20 | + elif (bmi > 18.5) and (bmi < 24.9): |
| 21 | + messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Normal') |
| 22 | + elif (bmi > 24.9) and (bmi < 29.9): |
| 23 | + messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Overweight') |
| 24 | + elif (bmi > 29.9): |
| 25 | + messagebox.showinfo('bmi-pythonguides', f'BMI = {bmi} is Obesity') |
| 26 | + else: |
| 27 | + messagebox.showerror('bmi-pythonguides', 'something went wrong!') |
| 28 | + |
| 29 | +ws = Tk() |
| 30 | +ws.title('PythonGuides') |
| 31 | +ws.geometry('400x300') |
| 32 | +ws.config(bg='#686e70') |
| 33 | + |
| 34 | +var = IntVar() |
| 35 | + |
| 36 | +frame = Frame( |
| 37 | + ws, |
| 38 | + padx=10, |
| 39 | + pady=10 |
| 40 | +) |
| 41 | +frame.pack(expand=True) |
| 42 | + |
| 43 | + |
| 44 | +age_lb = Label( |
| 45 | + frame, |
| 46 | + text="Enter Age (2 - 120)" |
| 47 | +) |
| 48 | +age_lb.grid(row=1, column=1) |
| 49 | + |
| 50 | +age_tf = Entry( |
| 51 | + frame, |
| 52 | +) |
| 53 | +age_tf.grid(row=1, column=2, pady=5) |
| 54 | + |
| 55 | +gen_lb = Label( |
| 56 | + frame, |
| 57 | + text='Select Gender' |
| 58 | +) |
| 59 | +gen_lb.grid(row=2, column=1) |
| 60 | + |
| 61 | +frame2 = Frame( |
| 62 | + frame |
| 63 | +) |
| 64 | +frame2.grid(row=2, column=2, pady=5) |
| 65 | + |
| 66 | +male_rb = Radiobutton( |
| 67 | + frame2, |
| 68 | + text = 'Male', |
| 69 | + variable = var, |
| 70 | + value = 1 |
| 71 | +) |
| 72 | +male_rb.pack(side=LEFT) |
| 73 | + |
| 74 | +female_rb = Radiobutton( |
| 75 | + frame2, |
| 76 | + text = 'Female', |
| 77 | + variable = var, |
| 78 | + value = 2 |
| 79 | +) |
| 80 | +female_rb.pack(side=RIGHT) |
| 81 | + |
| 82 | +height_lb = Label( |
| 83 | + frame, |
| 84 | + text="Enter Height (cm) " |
| 85 | +) |
| 86 | +height_lb.grid(row=3, column=1) |
| 87 | + |
| 88 | +weight_lb = Label( |
| 89 | + frame, |
| 90 | + text="Enter Weight (kg) ", |
| 91 | + |
| 92 | +) |
| 93 | +weight_lb.grid(row=4, column=1) |
| 94 | + |
| 95 | +height_tf = Entry( |
| 96 | + frame, |
| 97 | +) |
| 98 | +height_tf.grid(row=3, column=2, pady=5) |
| 99 | + |
| 100 | +weight_tf = Entry( |
| 101 | + frame, |
| 102 | +) |
| 103 | +weight_tf.grid(row=4, column=2, pady=5) |
| 104 | + |
| 105 | +frame3 = Frame( |
| 106 | + frame |
| 107 | +) |
| 108 | +frame3.grid(row=5, columnspan=3, pady=10) |
| 109 | + |
| 110 | +cal_btn = Button( |
| 111 | + frame3, |
| 112 | + text='Calculate', |
| 113 | + command=calculate_bmi |
| 114 | +) |
| 115 | +cal_btn.pack(side=LEFT) |
| 116 | + |
| 117 | +reset_btn = Button( |
| 118 | + frame3, |
| 119 | + text='Reset', |
| 120 | + command=reset_entry |
| 121 | +) |
| 122 | +reset_btn.pack(side=LEFT) |
| 123 | + |
| 124 | +exit_btn = Button( |
| 125 | + frame3, |
| 126 | + text='Exit', |
| 127 | + command=lambda:ws.destroy() |
| 128 | +) |
| 129 | +exit_btn.pack(side=RIGHT) |
| 130 | + |
| 131 | +ws.mainloop() |
0 commit comments