简体   繁体   中英

String object not callable, GUI BMI Python calculator

Help with the following code for a project that I need to complete for class. I am stuck with the very last part. I am getting an error. Below is my entire code:

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()

I am getting the following error message when I use 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

Can someone help with this? Why am I getting this error when using the button?

I guess, you need to assign value to the field, not call it.

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.'

Please note the assignment sign in the snippet above.

The problem is that you set category to a string with self.category = 'You are Normal ' and then call it several times. Hence the message. Here is a more correct version of the function.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM