python - Tkinter 中其他类的变量
问题描述
我已经尝试了很长时间,并准备好每一个相关的帖子,但我快要放弃了。我想在不同的类中使用bmr_result
,BMR_class
调用working_cals
它有一个调用函数calories
,它位于代码示例的底部。我已经在代码中添加了注释以显示我想要做什么,但我真的需要一些指导来了解我需要在__init__
每个类中添加什么以及以后如何调用它bmr_result
。提前致谢。
PS 我已经删除了文本框、标签和诸如此类的东西,以使其不那么笨重,但是所有的计算都完美地工作,我只需要知道如何跨类使用变量。
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)
标签、文本框等...
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)
解决方案
推荐阅读
- python-3.x - 如何使用 repo 包安装最新版本的 pip
- python - 用多个用户输入填充字典
- flutter - 应用程序终止时无法访问本地数据库
- xamarin - Xamarin Android Android.OS.FileUriExposedException:
- laravel - laravel 6.* 中的错误空参数 route() 辅助函数
- laravel - 带有谷歌图表的 Laravel 6.0 无法显示数据
- angular - Angular中组件之间的通信
- mongodb - Jmeter 无法将文档插入 MongoDB
- arrays - 未知测试用例的重复和丢失数字问题失败
- c++ - pybind11:卡在未解决的重载函数类型上