我正在嘗試撰寫一段代碼,該代碼從用戶那里獲取輸入,然后傳遞給執行某些操作的多個程式。下面是我正在使用的代碼,作為試驗,我正在獲取輸入并嘗試在函式之外列印它,因為未定義“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")
uj5u.com熱心網友回復:
因為 Input_path 是在方法中定義的,并且您正在呼叫 Class 外部的路徑,所以它無法訪問它,一種解決方案是將 Input_path 設為 Global 以便可以在外部呼叫它
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461236.html
標籤:Python python-3.x python-2.7 tkinter python-3.6
上一篇:condacreateenvironment命令給出'發現沖突!尋找不兼容的軟體包。
下一篇:如何為同一屬性設定多個名稱
