我正在尝试使用Python和Tkinter界面图形制作BMI计算器
这是我的代码:
def createButtons(self):
self.buttonCalculate = tk.Button(self)
self.buttonCalculate["text"]="Calculate"
self.buttonCalculate["fg"]="red"
self.buttonCalculate["bg"]="yellow"
self.buttonCalculate["font"]=("arial","16","italic","bold")
self.buttonCalculate["height"]=3
self.buttonCalculate["width"]=15
self.buttonCalculate["command"]=self.actionPrint
self.buttonCalculate.pack(side="top")
self.buttonExit = Button(self,text="Exit",fg="red",bg="blue",command=root.destroy)
self.buttonExit.pack(side="bottom")
def createLabels(self):
self.label = tk.Label(self)
self.label["font"]=("arial","16","italic","bold")
self.label["height"]=3
self.label["width"]=45
self.label["text"]="Type your weight(kg) and your height(cm) respectively"
self.label.pack(side="left")
def dataEntry1(self):
self.edit = tk.Entry(self.master,width=35)
self.edit.grid(row=2,column=0)
def dataEntry2(self):
self.edit = tk.Entry(self.master,width=35)
self.edit.grid(row=1,column=0)
def actionPrint(self):
print("Test")
def dataCalculation(self):
dataCalculation=(self.dataEntry1/(self.dataEntry2*self.dataEntry2))
print("your BMI is: ", dataCalculation)
root = tk.Tk()
TypeError:*:'instancemethod'和以下不支持的操作数类型 'instancemethod'
没有Erro出现,但是它不起作用,我没有得到我要计算的BMI值
如何获取数据?
答案 0 :(得分:0)
发生标题错误是因为您尝试在方法之间进行数学运算。
您要做的是使用Entry
中包含的值进行操作。
您可能应该有类似的东西:
def createButtons(self):
self.buttonCalculate = tk.Button(self)
[...]
self.buttonCalculate["command"] = self.dataCalculation
[...]
# set the entries: these two method are likely called in a ``__init__`` or setup method
def set_data_entry1(self):
self.edit1 = tk.Entry(self.master,width=35)
self.edit1.grid(row=2,column=0)
def set_data_entry2(self):
self.edit2 = tk.Entry(self.master,width=35)
self.edit2.grid(row=1,column=0)
# callback for e.g. a button: get the values of the two entries, converted them to float and do the operations
def dataCalculation(self):
dataCalculation=(self.get_data_entry1())/(self.get_data_entry2() * self.get_data_entry2())
print("your BMI is: ", dataCalculation)
def get_data_entry1(self):
return float(self.edit1.get())
def get_data_entry2(self):
return float(self.edit2.get())