from tkinter import *
class main():
def __init__(self):
self.root = Tk()
self.root.geometry("500x500")
self.root.title("BMI calculator")
self.root.config(bg="#ff9f1c")
self.WEIGHT = Entry(self.root)
self.WEIGHT.place(x=60,y=50)
self.WEIGHT.config(bg="#94fbab")
self.w_Label = Label(self.root,text="Weight(kg):")
self.w_Label.place(x=60,y=20)
self.w_Label.config(bg="#cbf3f0")
self.HEIGHT = Entry(self.root)
self.HEIGHT.place(x=300,y=50)
self.HEIGHT.config(bg="#94fbab")
self.w = self.WEIGHT.get()
self.h = self.HEIGHT.get()
self.h_Label = Label(self.root,text="Height(cm):")
self.h_Label.place(x=300,y=20)
self.h_Label.config(bg="#cbf3f0")
self.calculate(self.w,self.h)
self.BMI = Label(self.root,text=self._BMI,bg="#ffe74c")
self.BMI.place(x=215,y=200)
self.BMI.config(width=10)
self.BMI_Label = Label(self.root,bg="#ff99c8",text="Your BMI:",font=("Helvetica",9))
self.BMI_Label.place(x=215,y=170)
self.BMI_Label.config(width=10)
self.w = self.WEIGHT.get()
self.h = self.HEIGHT.get()
button = Button(self.root,text="Calculate",bg="#35a7ff",command=self.calculate(self.w,self.h))
button.place(x=220,y=250)
self.root.mainloop()
def calculate(self,w,h):
try:
_h = h / 100
h_ = _h * _h
result = round(w / h_)
self._BMI = result
self.BMI.update()
except:
self._BMI = 0
輸入後BMI不會更新,有人會嗎?
你的self.w跟self.h只有在__ini__裡更新過一次而已。
你必須在觸發calculate時重新從self.WEIGHT與self.HEIGHT取值