Home > OS >  Python code to get multiple input from a pop up window and reuse it
Python code to get multiple input from a pop up window and reuse it

Time:04-11

I'm trying to write a piece of code that gets input from the user and is then passed to multiple pieces of programs that do certain operations. Below is the code I'm using, For a trial, I'm getting the input and trying to print it outside the function which throws an error as 'Input_Path' is not defined.

Could somebody help me out here?

    import tkinter as tk
    import openpyxl
    
    class App(tk.Frame):
        def __init__(self,master=None,**kw):
            #Create a blank dictionary
            self.answers = {}
            tk.Frame.__init__(self,master=master,**kw)
    
            tk.Label(self,text="Give Input Sheet Path").grid(row=0,column=0)
            self.question1 = tk.Entry(self)
            self.question1.grid(row=0,column=1)
    
            tk.Label(self,text="Give Output Sheet Path").grid(row=1,column=0)
            self.question2 = tk.Entry(self)
            self.question2.grid(row=1,column=1)
    
            tk.Button(self,text="Feed into Program",command = self.collectAnswers).grid(row=2,column=1)
    
        def collectAnswers(self):
            self.answers['Input_Path'] = self.question1.get()
            self.answers['Output_Path'] = self.question2.get()
            Input_Path = self.answers['Output_Path']
            Output_Path = self.question2.get()
            functionThatUsesAnswers(self.answers)
            
    
    def functionThatUsesAnswers(answers):
    print("Given Input Path ", answers['Input_Path'])
    print("Given Output Path ", answers['Output_Path'])
    quit()
    def quit():
        root.destroy()        
    if __name__ == '__main__':
        root = tk.Tk()
        App(root).grid()
        root.mainloop()
    
    print(Input_Path)
    wb = openpyxl.load_workbook(f'r"{Input_Path}"') # trying to open the input sheet from the below path
    #wb = openpyxl.load_workbook(r"C:\Users\xx'x\xx.x\Input_Sheet.xlsx")

CodePudding user response:

Because the Input_path is defined in method and you are calling the path outside the Class so it cant access it, one solution is make that Input_path as Global so it can be called outside

  • Related