iT邦幫忙

0

BMI 計算器遇到問題,無法更新

  • 分享至 

  • xImage
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

https://ithelp.ithome.com.tw/upload/images/20220621/20149956FfjINvErHc.jpg
輸入後BMI不會更新,有人會嗎?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
frogsoul
iT邦研究生 4 級 ‧ 2022-06-22 08:32:16

你的self.w跟self.h只有在__ini__裡更新過一次而已。
你必須在觸發calculate時重新從self.WEIGHT與self.HEIGHT取值

我要發表回答

立即登入回答