簡體   English   中英

字符串對象不可調用,GUI BMI Python計算器

[英]String object not callable, GUI BMI Python calculator

為我需要在課堂上完成的項目提供以下代碼的幫助。 我停留在最后一部分。 我收到一個錯誤。 下面是我的整個代碼:

import tkinter
import tkinter.messagebox

class BMI_gui:
    def __init__(self):

        #create the main window
        self.main_window = tkinter.Tk()
        #create 2 frames for grouping
        self.top_frame = tkinter.Frame(self.main_window)
        self.bottom_frame = tkinter.Frame(self.main_window)
        #create widgets for top frame
        self.weight_label = tkinter.Label(self.top_frame, text = 'Please inputyour weight in whole pounds: ')
        self.weight_entry = tkinter.Entry(self.top_frame, width=10)

        self.height_label = tkinter.Label(self.top_frame, text = 'Please input your height in inches: ')
        self.height_entry = tkinter.Entry(self.top_frame, width=10)


        #pack top frame
        self.weight_label.pack(side='left')
        self.weight_entry.pack(side='left')

        self.height_label.pack(side='left')
        self.height_entry.pack(side='left')

        #create button widgets for bottom frame
        self.calc_button = tkinter.Button(self.bottom_frame, text = 'Calculate your BMI', command=self.calcBMI)
        self.quit_button = tkinter.Button(self.bottom_frame, text = 'Quit',
        command = self.main_window.destroy)
        #packing bottom buttons and frames
        self.calc_button.pack(side='left')
        self.quit_button.pack(side = 'left')
        self.top_frame.pack()
        self.bottom_frame.pack()

        tkinter.mainloop()

    def calcBMI(self):
       #get the weight and height values.
        weight = int(self.weight_entry.get())
        height = int(self.height_entry.get())

        #BMI calc
        bmi = float((weight * 703)/(height * height))

        self.category = 'You are Normal '
        if bmi >= 30:
            self.category ('Your BMI describes you as Obese.')
        elif bmi <30 and bmi >=25:
            self.category ('Your BMI describes you as Overweight.')
        elif bmi <25 and bmi >=18.5:
            self.category ('Your BMI describes you as Normal.')
        else:
            self.category ('Your BMI describes you as Underweight.')

         # Display the results
        tkinter.messagebox.showinfo('BMI Calulation','Your BMI is: '+
        format(bmi,'.1f') + str(self.category))


bmi_calc = BMI_gui()

使用self.calc_button時出現以下錯誤消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"C:\Users\oliva\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 
1699, in __call__
    return self.func(*args)
  File "F:\ITSE 1429\AOProjectFour.py", line 48, in calcBMI
    self.category ('Your BMI describes you as Obese.')
TypeError: 'str' object is not callable

有人可以幫忙嗎? 為什么在使用按鈕時出現此錯誤?

我想,您需要為字段分配值,而不是調用它。

if bmi >= 30:
    self.category = 'Your BMI describes you as Obese.'
elif bmi <30 and bmi >=25:
    self.category = 'Your BMI describes you as Overweight.'
elif bmi <25 and bmi >=18.5:
    self.category = 'Your BMI describes you as Normal.'
else:
    self.category = 'Your BMI describes you as Underweight.'

請注意上面代碼段中的作業標志。

問題是您將category設置為具有self.category = 'You are Normal '的字符串,然后多次調用它。 因此,消息。 這是該功能的更正確版本。

def calcBMI(self):
   #get the weight and height values.
    weight = int(self.weight_entry.get())
    height = int(self.height_entry.get())

    #BMI calc
    bmi = float((weight * 703)/(height * height))

    if bmi >= 30:
        self.category ='Your BMI describes you as Obese.'
    elif bmi <30 and bmi >=25:
        self.category = 'Your BMI describes you as Overweight.'
    elif bmi <25 and bmi >=18.5:
        self.category = 'Your BMI describes you as Normal.'
    else:
        self.category = 'Your BMI describes you as Underweight.'

     # Display the results
    tkinter.messagebox.showinfo('BMI Calulation','Your BMI is: '+ 
    format(bmi,'.1f') + self.category)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM