我已经尝试了很长时间,并且准备好每个相关的帖子,但是我快要放弃了。我想在另一个类bmr_result
中使用BMR_class
中的working_cals
,该类在代码示例的底部有一个名为calories
的函数。我已经在代码中添加了注释,以显示我想做什么,但是我确实需要一些指导,以明确说明每个类的__init__
中到底需要输入什么以及以后如何调用此bmr_result
上。
预先感谢。
P.S。我删除了文本框,标签和其他内容,以使其不那么笨重,但是所有计算都可以正常工作,我只需要知道如何在各个类之间使用变量即可。
class theog(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side='top', fill='both', expand= True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, BMR_class, working_cals):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(StartPage)
def show_frame(self, controller):
frame = self.frames[controller]
frame.tkraise()
class BMR_class(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.bmr_result = 0
tk.Button(self, text="Calculate BMR!", width=15, command=self.bmr_method).grid(row=2,
column=0, sticky='W')
标签,文本框等...
tk.Button(self, width=15, text="2nd Step!",
command=lambda: controller.show_frame(working_cals)).grid(row=3, column=0, sticky='W')
def bmr_method(self, Entry=None):
if self.text_height.get() and self.text_weight.get() and self.text_age.get() and self.var1.get() == 'male':
bh = float(self.text_height.get()) * 5.0033
bw = float(self.text_weight.get()) * 13.7516
ba = float(self.text_age.get()) * 6.7550
self.bmr_result = float(66.4730 + bh + bw - ba) #***`I NEED THIS RESULT TO BE USED IN THE FINAL FUNCTION`***
这是我需要使用的结果。
self.resultvar.set('Your BMR is: ' + str(self.bmr_result))
elif self.text_height.get() and self.text_weight.get() and self.text_age.get() and self.var1.get() == 'female':
bh = float(self.text_height.get()) * 1.8496
bw = float(self.text_weight.get()) * 9.5634
ba = float(self.text_age.get()) * 4.6756
self.bmr_result = float(655.095 + bh + bw - ba)
self.resultvar.set('Your BMR is:' + str(self.bmr_result) +'\n press continue to find out \n your maintenance calories')
else:
'Please ensure all information has been entered and click again'
self.resultvar.set('Please ensure all \n information has been \n entered and click again')
class working_cals(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
标签,文本框,ETC ...
def calories(self, entry=None):
work_cals = (float(self.var2.get()) * float(self.hours.get())) / 7
activity_cals = steps_cals + work_cals + self.bmr_result #HERE I NEED TO USE IT
print(activity_cals)