Python 只接受一个参数(给定0) class bmicaller(): #为bmi计算器GUI创建的类,并处理这些数字(让人头疼)# 定义初始(自我,主): self.heightcm=DoubleVar() self.weightkg=DoubleVar() self.master=master 自主几何(“250x200+100+200”) self.master.title(“BMI计算器”) self.label2=Label(self.master,text='欢迎使用BMI计算器',fg='red').grid(行=0,列=0) self.label2=Label(self.master,text='请以厘米为单位输入您的身高',fg='black').grid(行=3,列=0) self.label2=Label(self.master,text='请以千克为单位输入您的体重',fg='black').grid(行=4,列=0) self.myheight=Entry(self.master,textvariable=self.heightcm).grid(行=3,列=1) self.myweight=Entry(self.master,textvariable=self.weightkg).grid(行=4,列=1) self.button4=按钮(self.master,text=“计算体重指数”,fg='red',command=self.bmiculation).grid(行=7,列=0) self.button5=按钮(self.master,text=“Exit”,fg='red',command=self.Exit).grid(行=9,列=0) def BMI计算(自): bmiheight=self.heightcm.get() 打印高度 bmiweight=self.weightkg.get() 体重指数=浮动((体重指数)/(身高指数/100)**2)) self.bmi=bmi 打印体重指数 self.label1=Label(self.master,text=)您的体重指数是%.2f'%BMI'。网格(行=5,列=0) 如果bmi为18.5且bmi=25且bmi=30: self.label5=Label(self.master,text='This places you in the footbality group',fg='red').grid(行=6,列=0) totalindex=‘肥胖’ self.totalindex=totalindex 如果bmi>0且bmi
我想我已经发现了你的错误 您通过以下方式将用作命令的函数传递给Python 只接受一个参数(给定0) class bmicaller(): #为bmi计算器GUI创建的类,并处理这些数字(让人头疼)# 定义初始(自我,主): self.heightcm=DoubleVar() self.weightkg=DoubleVar() self.master=master 自主几何(“250x200+100+200”) self.master.title(“BMI计算器”) self.label2=Label(self.master,text='欢迎使用BMI计算器',fg='red').grid(行=0,列=0) self.label2=Label(self.master,text='请以厘米为单位输入您的身高',fg='black').grid(行=3,列=0) self.label2=Label(self.master,text='请以千克为单位输入您的体重',fg='black').grid(行=4,列=0) self.myheight=Entry(self.master,textvariable=self.heightcm).grid(行=3,列=1) self.myweight=Entry(self.master,textvariable=self.weightkg).grid(行=4,列=1) self.button4=按钮(self.master,text=“计算体重指数”,fg='red',command=self.bmiculation).grid(行=7,列=0) self.button5=按钮(self.master,text=“Exit”,fg='red',command=self.Exit).grid(行=9,列=0) def BMI计算(自): bmiheight=self.heightcm.get() 打印高度 bmiweight=self.weightkg.get() 体重指数=浮动((体重指数)/(身高指数/100)**2)) self.bmi=bmi 打印体重指数 self.label1=Label(self.master,text=)您的体重指数是%.2f'%BMI'。网格(行=5,列=0) 如果bmi为18.5且bmi=25且bmi=30: self.label5=Label(self.master,text='This places you in the footbality group',fg='red').grid(行=6,列=0) totalindex=‘肥胖’ self.totalindex=totalindex 如果bmi>0且bmi,python,Python,我想我已经发现了你的错误 您通过以下方式将用作命令的函数传递给按钮类: class bmicalculator(): #class created for the bmi calculator GUI and processing the numbers (pain in the ass to make)# def __init__(self,master): self.heightcm=DoubleVar() self.weight
按钮
类:
class bmicalculator():
#class created for the bmi calculator GUI and processing the numbers (pain in the ass to make)#
def __init__(self,master):
self.heightcm=DoubleVar()
self.weightkg=DoubleVar()
self.master=master
self.master.geometry('250x200+100+200')
self.master.title('BMI Calculator')
self.label2=Label(self.master,text='Welcome to the BMI Calculator',fg='red').grid(row=0,column=0)
self.label2=Label(self.master,text='Please enter your height in centimetres',fg='black').grid(row=3,column=0)
self.label2=Label(self.master,text='Please enter your weight in kilograms',fg='black').grid(row=4,column=0)
self.myheight=Entry(self.master,textvariable=self.heightcm).grid(row=3,column=1)
self.myweight=Entry(self.master,textvariable=self.weightkg).grid(row=4,column=1)
self.button4=Button(self.master,text="Calculate BMI",fg='red',command=self.bmicalculation).grid(row=7,column=0)
self.button5=Button(self.master,text="Exit",fg='red',command=self.exit).grid(row=9,column=0)
def bmicalculation(self):
bmiheight=self.heightcm.get()
print bmiheight
bmiweight=self.weightkg.get()
bmi= float((bmiweight)/((bmiheight / 100)**2))
self.bmi = bmi
print bmi
self.label1=Label(self.master,text='Your BMI is %.2f' % bmi).grid(row=5,column=0)
if bmi <= 18.5:
self.label2=Label(self.master,text='This places you in the underweight group.',fg='blue').grid(row=6,column=0)
totalindex = 'underweight'
self.totalindex = totalindex
elif bmi >18.5 and bmi <25:
self.label3=Label(self.master,text='This places you in the healthy weight group.',fg='green').grid(row=6,column=0)
totalindex = 'healthy'
self.totalindex = totalindex
elif bmi >= 25 and bmi < 30:
self.label4=Label(self.master,text='This places you in the overweight group.',fg='orange').grid(row=6,column=0)
totalindex = 'overweight'
self.totalindex = totalindex
elif bmi >=30:
self.label5=Label(self.master,text='This places you in the obese group.',fg='red').grid(row=6,column=0)
totalindex = 'obese'
self.totalindex = totalindex
if bmi >0 and bmi <999999999999999999999:
self.button6=Button(self.master,text="Store Data",fg='red',command=self.dynamic_data_entry).grid(row=8,column=0)
def dynamic_data_entry(self):
#this is what adds the data to the database. Bmi has to be changed to the value of bmi and weightclass has to be change to the weightclass
timestamp = str(datetime.datetime.now().date())
bodymassindex = self.bmi
weightclass = self.totalindex
c.execute("INSERT INTO BMIStorage (timestamp, bodymassindex, weightclass) VALUES (?, ?, ?)",(timestamp, bodymassindex, weightclass))
conn.commit()
create_table()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS BMIStorage(timestamp TEXT,bmi REAL,weightclass TEXT)')
但是,如果您直接传递该方法,那么一旦该方法本身需要一个隐式参数,即self
,就会导致所得到的错误
这个self
表示类的对象。但是,当您直接传递方法时,不涉及任何对象
因此,您应该这样做:
self.button6=Button(...., command=self.dynamic_data_entry).grid(row=8,column=0)
这样,您告诉按钮
类从对象调用方法,在本例中,self
参数将存在,因为实例化的对象存在
确保您理解command=self.dynamic\u data\u entry
和command=obj.dynamic\u data\u entry
之间的区别,我想我已经发现了您的错误
您通过以下方式将用作命令的函数传递给按钮
类:
class bmicalculator():
#class created for the bmi calculator GUI and processing the numbers (pain in the ass to make)#
def __init__(self,master):
self.heightcm=DoubleVar()
self.weightkg=DoubleVar()
self.master=master
self.master.geometry('250x200+100+200')
self.master.title('BMI Calculator')
self.label2=Label(self.master,text='Welcome to the BMI Calculator',fg='red').grid(row=0,column=0)
self.label2=Label(self.master,text='Please enter your height in centimetres',fg='black').grid(row=3,column=0)
self.label2=Label(self.master,text='Please enter your weight in kilograms',fg='black').grid(row=4,column=0)
self.myheight=Entry(self.master,textvariable=self.heightcm).grid(row=3,column=1)
self.myweight=Entry(self.master,textvariable=self.weightkg).grid(row=4,column=1)
self.button4=Button(self.master,text="Calculate BMI",fg='red',command=self.bmicalculation).grid(row=7,column=0)
self.button5=Button(self.master,text="Exit",fg='red',command=self.exit).grid(row=9,column=0)
def bmicalculation(self):
bmiheight=self.heightcm.get()
print bmiheight
bmiweight=self.weightkg.get()
bmi= float((bmiweight)/((bmiheight / 100)**2))
self.bmi = bmi
print bmi
self.label1=Label(self.master,text='Your BMI is %.2f' % bmi).grid(row=5,column=0)
if bmi <= 18.5:
self.label2=Label(self.master,text='This places you in the underweight group.',fg='blue').grid(row=6,column=0)
totalindex = 'underweight'
self.totalindex = totalindex
elif bmi >18.5 and bmi <25:
self.label3=Label(self.master,text='This places you in the healthy weight group.',fg='green').grid(row=6,column=0)
totalindex = 'healthy'
self.totalindex = totalindex
elif bmi >= 25 and bmi < 30:
self.label4=Label(self.master,text='This places you in the overweight group.',fg='orange').grid(row=6,column=0)
totalindex = 'overweight'
self.totalindex = totalindex
elif bmi >=30:
self.label5=Label(self.master,text='This places you in the obese group.',fg='red').grid(row=6,column=0)
totalindex = 'obese'
self.totalindex = totalindex
if bmi >0 and bmi <999999999999999999999:
self.button6=Button(self.master,text="Store Data",fg='red',command=self.dynamic_data_entry).grid(row=8,column=0)
def dynamic_data_entry(self):
#this is what adds the data to the database. Bmi has to be changed to the value of bmi and weightclass has to be change to the weightclass
timestamp = str(datetime.datetime.now().date())
bodymassindex = self.bmi
weightclass = self.totalindex
c.execute("INSERT INTO BMIStorage (timestamp, bodymassindex, weightclass) VALUES (?, ?, ?)",(timestamp, bodymassindex, weightclass))
conn.commit()
create_table()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS BMIStorage(timestamp TEXT,bmi REAL,weightclass TEXT)')
但是,如果您直接传递该方法,那么一旦该方法本身需要一个隐式参数,即self
,就会导致所得到的错误
这个self
表示类的对象。但是,当您直接传递方法时,不涉及任何对象
因此,您应该这样做:
self.button6=Button(...., command=self.dynamic_data_entry).grid(row=8,column=0)
这样,您告诉按钮
类从对象调用方法,在本例中,self
参数将存在,因为实例化的对象存在
确保您了解command=self.dynamic\u data\u entry
和command=obj.dynamic\u data\u entry
是您的功能
是类的方法
是的,函数是类的方法class@BenjaminInverno您能在调用此函数的地方发布代码吗?添加self
创建表格()
将self添加到这两个类中,但现在这两个类的错误都相同。您的功能是类的方法吗class@BenjaminInverno您能在调用此函数的地方发布代码吗?添加self
以创建表()
function-too->创建表(self)
将self添加到这两个选项中,但现在两个选项都出现了相同的错误