1

I have been trying for so long and ready every related post but I'm close to giving up. I want to use bmr_result from the BMR_class in a different class, called working_cals which has a function called calories which is at the bottom of the code sample. I have added comments to the code to show what I want to do, but I really need some guidance on what exactly I need to put in the __init__ of each class and how I can call this bmr_result later on. Thanks in advance.

P.S. I have removed textboxes, labels, and whatnot to make it less bulky, but all of the calculations work perfectly, I just need to know how to use variables across classes.

 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')

LABELS, TEXTBOXES ETC...

        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`***

This is where the result I need to use is.

            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)

LABELS, TEXTBOXES, 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)
Remitto
  • 53
  • 10
  • 2
    Have you see this? https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class – Bryan Oakley Sep 26 '19 at 04:11
  • I actually read both of those, but just had another look at @Legorooj suggestion and I've finally understood it. THanks so much – Remitto Sep 27 '19 at 06:04

0 Answers0