为我需要为课程完成的项目提供以下代码的帮助。我坚持最后一部分。我收到了一个错误。以下是我的整个代码:
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
有人可以帮忙吗?使用按钮时为什么会出现此错误?
答案 0 :(得分:1)
我想,你需要为字段赋值,而不是调用它。
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.'
请注意上面代码段中的作业签名。
答案 1 :(得分:0)
问题是您将类别设置为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)