我正在使用tkinter创建一个程序,该程序将文本文件中的数据显示到Text小部件中。但是,当我尝试运行程序时,我收到'NameError:name'text_identity'未定义'。任何人都可以解释为什么会出现这个错误,以及它是如何修复的?
from tkinter import *
from tkinter import ttk
from tkinter import font
from tkinter.filedialog import askopenfilename
class Manager:
def __init__(self, root):
#The frame for the GUI itself
mainframe = ttk.Frame(root, relief=SUNKEN, padding="3 10 12 12")
mainframe.grid(column=0, row=0, columnspan=10, rowspan=10, sticky="NW")
button_load= ttk.Button(mainframe, text="Load",command=self.OpenFile)
button_load.grid(row=35, column=17, sticky = "NE", padx=5, pady=10)
text_identity = Text(mainframe, width = 15, height = 2)
text_identity.grid(column=8, row=5, sticky=(N,W))
def OpenFile(self):
File1= askopenfilename()
File2= open(File1,'r')
text_identity.insert("1.0",File2.read())
File2.close()
def main():
root = Tk()
Manager(root)
root.title("Data Management")
root.mainloop()
if __name__ == main():
main()