Python Forum

Full Version: Bringing function out of class into main loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,
Does anybody know how to bring function out of a class into main loop?
For my code, I was not able to call the function lol1 into the external main loop(dispense) for dispensing solution. It feels like there's something wrong with my logic.
Any help will kindly be appreciated!

import tkinter as tk
import time



class BackGroundFrame(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.shared_data = {
            "name1": tk.StringVar(),
            "name2": tk.StringVar(),
            "name3": tk.StringVar(),
            "name4": tk.StringVar(),
            }
        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, ManualPage):    
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            
        self.show_frame(StartPage)
        
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        
    def get_page(self, page_class):
        return self.frames[page_class]

        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        
        instruction = tk.Label(self, text="Welcome to Dispensing GUI")
        instruction.grid(row=0 , column=0, columnspan=3, sticky="W")
        
        tk.Label(self, text="        ").grid(row=1, column=0, sticky="W")
        
        tk.Label(self, text="Solutions to Pumps Allocations").grid(row=2, column=0, columnspan=10, sticky="W")
        
        self.typeofsolution1 = tk.Entry(self, textvariable=self.controller.shared_data["name1"], width=12)  # tk.Entry widget function 
        self.typeofsolution1.grid(row=3, column=0, sticky="W")
        tk.Label(self, text="Pump 1").grid(row=4, column=0, columnspan=2, sticky="W") 
         
        tk.Label(self, text="    ").grid(row=3, column=1, sticky="W")
        
        self.typeofsolution2 = tk.Entry(self, textvariable=self.controller.shared_data["name2"], width=12)  # tk.Entry widget function 
        self.typeofsolution2.grid(row=3, column=2, sticky="W")
        tk.Label(self, text="Pump 2").grid(row=4, column=2, sticky="W")
        
        tk.Label(self, text="    ").grid(row=3, column=3, sticky="W")
        
        self.typeofsolution3 = tk.Entry(self, textvariable=self.controller.shared_data["name3"], width=12)
        self.typeofsolution3.grid(row=3, column=4, sticky="W")
        tk.Label(self, text="Pump 3").grid(row=4, column=4, columnspan=2, sticky="W") 
        
        tk.Label(self, text="    ").grid(row=3, column=5, sticky="W")
        
        self.typeofsolution4 = tk.Entry(self, textvariable=self.controller.shared_data["name4"], width=12)
        self.typeofsolution4.grid(row=3, column=6, sticky="W")
        tk.Label(self, text="Pump 4").grid(row=4, column=6, columnspan=2, sticky="W")
        
        self.submitManual = tk.Button(self, text="Manual", command=lambda:controller.show_frame(ManualPage))
        self.submitManual.grid(row=8, rowspan=5, column=0, columnspan=4, sticky="W")
        
        self.submitAutomatic = tk.Button(self, text="Automatic")
        self.submitAutomatic.grid(row=8, rowspan=5, column=1, columnspan=2, sticky="W")
            
        
class ManualPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
         
        self.manualtitle = tk.Label(self, text="Welcome to Manual Dispensing")
        self.manualtitle.grid(row=0 , column=0, columnspan=6, sticky="W")
        
        tk.Label(self, text="Solution Name    ").grid(row=1, column=0, sticky="W")
        tk.Label(self, text="Time Input(s)     ").grid(row=1, column=2, sticky="w")
        tk.Label(self, text="Dispense").grid(row=1, column=3, sticky="W")
       
        
        
        self.submitlol = tk.Button(self, text="Back to Main Menu", command=lambda:controller.show_frame(StartPage))
        self.submitlol.grid(row=10, rowspan=5, column=0, columnspan=4, sticky="W")
        
        self.timepump1 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump1.grid(row=2, column=1, columnspan=10, sticky="w")
        
        self.timepump2 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump2.grid(row=4, column=1, columnspan=10, sticky="w")
        
        self.timepump3 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump3.grid(row=6, column=1, columnspan=10, sticky="w")
        
        self.timepump4 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump4.grid(row=8, column=1, columnspan=10, sticky="w")
        
        self.submit1 = tk.Button(self, text="Dispense 1",command=self.dispense)
        self.submit1.grid(row=2, column=3, columnspan=10, sticky="w")
        
        self.submit2 = tk.Button(self, text="Dispense 2")
        self.submit2.grid(row=4, column=3, columnspan=10, sticky="w")
        
        self.submit3 = tk.Button(self, text="Dispense 3")
        self.submit3.grid(row=6, column=3, columnspan=10, sticky="w")
        
        self.submit4 = tk.Button(self, text="Dispense 4")
        self.submit4.grid(row=8, column=3, columnspan=10, sticky="w")
        
        self.action1 = tk.Button(self, text="Retrieve Solution Name",command=self.retrievesolutions)
        self.action1.grid(row=0, column=3, columnspan=3, sticky="W")
        
    def retrievesolutions(self):
        name1 = self.controller.shared_data["name1"].get()
        name2 = self.controller.shared_data["name2"].get()
        name3 = self.controller.shared_data["name3"].get()
        name4 = self.controller.shared_data["name4"].get()
        
        self.text1 = tk.Text(self, width=10, height=1)
        self.text1.grid(row=2, column=0, columnspan=10, sticky="W")
        
        
        self.text2 = tk.Text(self, width=10, height=1)
        self.text2.grid(row=4, column=0, columnspan=10, sticky="W")
        
        
        self.text3 = tk.Text(self, width=10, height=1)
        self.text3.grid(row=6, column=0, columnspan=10, sticky="w")
        
        
        self.text4 = tk.Text(self, width=10, height=1)
        self.text4.grid(row=8, column=0, columnspan=10, sticky="w")
        
        
        self.text1.delete(0.0,tk.END)
        self.text1.insert(0.0, name1)
        
        self.text2.delete(0.0,tk.END)
        self.text2.insert(0.0, name2)
        
        self.text3.delete(0.0,tk.END)
        self.text3.insert(0.0, name3)
        
        self.text4.delete(0.0,tk.END)
        self.text4.insert(0.0, name4)
        
        
        
        print(name1, name2, name3, name4)
        
    
    def lol1(self):
        solution="1" 
        t=self.timepump1.get()
        return solution,t
        
    def lol2(self):
       solution="2"
       t=self.timepump2.get()
       return solution,t

    def lol3(self):
        solution="2"
        t=self.timepump2.get()
        return solution,t
        
    

def dispense(self,solution,t):
    #solution,t=self.lol1
    if solution in ("1","2","3"):
        print("solution",solution,"time taken:", t)
        gpt ={"1":17,"2":18,"3":23,"4":24}
        print("GPIOPORTactivation:",gpt[solution])
        print("GPIO High") #GPIO.output(gpt[solution],HIGH)
        time.sleep(int(t))
        print("GPIO low")#GPIO.output(gpt[solution],LOW)

solution, t = ManualPage.self.lol1  
dispense(solution=solution,t=t)
            
    

        
app = BackGroundFrame()
app.mainloop()
with your code there is no GUI

you must indent
def dispense(self,solution,t):
import tkinter as tk
import time

class BackGroundFrame(tk.Tk):
 
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.shared_data = {
            "name1": tk.StringVar(),
            "name2": tk.StringVar(),
            "name3": tk.StringVar(),
            "name4": tk.StringVar(),
            }
        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, ManualPage):    
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
             
        self.show_frame(StartPage)
         
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
         
    def get_page(self, page_class):
        return self.frames[page_class]
 
         
class StartPage(tk.Frame):
 
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
         
        instruction = tk.Label(self, text="Welcome to Dispensing GUI")
        instruction.grid(row=0 , column=0, columnspan=3, sticky="W")
         
        tk.Label(self, text="        ").grid(row=1, column=0, sticky="W")
         
        tk.Label(self, text="Solutions to Pumps Allocations").grid(row=2, column=0, columnspan=10, sticky="W")
         
        self.typeofsolution1 = tk.Entry(self, textvariable=self.controller.shared_data["name1"], width=12)  # tk.Entry widget function 
        self.typeofsolution1.grid(row=3, column=0, sticky="W")
        tk.Label(self, text="Pump 1").grid(row=4, column=0, columnspan=2, sticky="W") 
          
        tk.Label(self, text="    ").grid(row=3, column=1, sticky="W")
         
        self.typeofsolution2 = tk.Entry(self, textvariable=self.controller.shared_data["name2"], width=12)  # tk.Entry widget function 
        self.typeofsolution2.grid(row=3, column=2, sticky="W")
        tk.Label(self, text="Pump 2").grid(row=4, column=2, sticky="W")
         
        tk.Label(self, text="    ").grid(row=3, column=3, sticky="W")
         
        self.typeofsolution3 = tk.Entry(self, textvariable=self.controller.shared_data["name3"], width=12)
        self.typeofsolution3.grid(row=3, column=4, sticky="W")
        tk.Label(self, text="Pump 3").grid(row=4, column=4, columnspan=2, sticky="W") 
         
        tk.Label(self, text="    ").grid(row=3, column=5, sticky="W")
         
        self.typeofsolution4 = tk.Entry(self, textvariable=self.controller.shared_data["name4"], width=12)
        self.typeofsolution4.grid(row=3, column=6, sticky="W")
        tk.Label(self, text="Pump 4").grid(row=4, column=6, columnspan=2, sticky="W")
         
        self.submitManual = tk.Button(self, text="Manual", command=lambda:controller.show_frame(ManualPage))
        self.submitManual.grid(row=8, rowspan=5, column=0, columnspan=4, sticky="W")
         
        self.submitAutomatic = tk.Button(self, text="Automatic")
        self.submitAutomatic.grid(row=8, rowspan=5, column=1, columnspan=2, sticky="W")
             
class ManualPage(tk.Frame):
 
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
          
        self.manualtitle = tk.Label(self, text="Welcome to Manual Dispensing")
        self.manualtitle.grid(row=0 , column=0, columnspan=6, sticky="W")
         
        tk.Label(self, text="Solution Name    ").grid(row=1, column=0, sticky="W")
        tk.Label(self, text="Time Input(s)     ").grid(row=1, column=2, sticky="w")
        tk.Label(self, text="Dispense").grid(row=1, column=3, sticky="W")     
         
        self.submitlol = tk.Button(self, text="Back to Main Menu", command=lambda:controller.show_frame(StartPage))
        self.submitlol.grid(row=10, rowspan=5, column=0, columnspan=4, sticky="W")
         
        self.timepump1 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump1.grid(row=2, column=1, columnspan=10, sticky="w")
         
        self.timepump2 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump2.grid(row=4, column=1, columnspan=10, sticky="w")
         
        self.timepump3 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump3.grid(row=6, column=1, columnspan=10, sticky="w")
         
        self.timepump4 = tk.Entry(self, width=10)  # entry widget function 
        self.timepump4.grid(row=8, column=1, columnspan=10, sticky="w")
         
        self.submit1 = tk.Button(self, text="Dispense 1",command=self.dispense)
        self.submit1.grid(row=2, column=3, columnspan=10, sticky="w")
         
        self.submit2 = tk.Button(self, text="Dispense 2")
        self.submit2.grid(row=4, column=3, columnspan=10, sticky="w")
         
        self.submit3 = tk.Button(self, text="Dispense 3")
        self.submit3.grid(row=6, column=3, columnspan=10, sticky="w")
         
        self.submit4 = tk.Button(self, text="Dispense 4")
        self.submit4.grid(row=8, column=3, columnspan=10, sticky="w")
         
        self.action1 = tk.Button(self, text="Retrieve Solution Name",command=self.retrievesolutions)
        self.action1.grid(row=0, column=3, columnspan=3, sticky="W")
         
    def retrievesolutions(self):
        name1 = self.controller.shared_data["name1"].get()
        name2 = self.controller.shared_data["name2"].get()
        name3 = self.controller.shared_data["name3"].get()
        name4 = self.controller.shared_data["name4"].get()
         
        self.text1 = tk.Text(self, width=10, height=1)
        self.text1.grid(row=2, column=0, columnspan=10, sticky="W")
         
         
        self.text2 = tk.Text(self, width=10, height=1)
        self.text2.grid(row=4, column=0, columnspan=10, sticky="W")
  
        self.text3 = tk.Text(self, width=10, height=1)
        self.text3.grid(row=6, column=0, columnspan=10, sticky="w")
 
        self.text4 = tk.Text(self, width=10, height=1)
        self.text4.grid(row=8, column=0, columnspan=10, sticky="w")
             
        self.text1.delete(0.0,tk.END)
        self.text1.insert(0.0, name1)
         
        self.text2.delete(0.0,tk.END)
        self.text2.insert(0.0, name2)
         
        self.text3.delete(0.0,tk.END)
        self.text3.insert(0.0, name3)
         
        self.text4.delete(0.0,tk.END)
        self.text4.insert(0.0, name4)

        print(name1, name2, name3, name4)
 
    def lol1(self):
        solution="1" 
        t=self.timepump1.get()
        return solution,t
         
    def lol2(self):
       solution="2"
       t=self.timepump2.get()
       return solution,t
 
    def lol3(self):
        solution="2"
        t=self.timepump2.get()
        return solution,t
 
    def dispense(self,solution,t):
        #solution,t=self.lol1
        if solution in ("1","2","3"):
            print("solution",solution,"time taken:", t)
            gpt ={"1":17,"2":18,"3":23,"4":24}
            print("GPIOPORTactivation:",gpt[solution])
            print("GPIO High") #GPIO.output(gpt[solution],HIGH)
            time.sleep(int(t))
            print("GPIO low")#GPIO.output(gpt[solution],LOW)
 
#solution, t = ManualPage.lol1  

app = BackGroundFrame()
app.mainloop()
#ManualPage.dispense(ManualPage, "", "")
ManualPage.lol1 ()