Python 从弹出窗口获取多个输入的代码 window 并重新使用它

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

我正在尝试编写一段代码,从用户那里获取输入,然后将其传递给执行某些操作的多个程序。 下面是我正在使用的代码,为了试用,我正在获取输入并尝试在函数外打印它,这会引发错误,因为 'Input_Path' 未定义。

有人可以帮我吗?

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

因为 Input_path 是在方法中定义的,而您正在调用 Class 之外的路径,所以它无法访问它,一种解决方案是将 Input_path 设置为全局,这样它就可以被叫到外面