from tkinter import*
from tkinter.filedialog import*
root=Tk()
root.geometry("400x100")
def test1():
with askopenfilename(title='上传文件',initialdir='d:',filetypes=[('文本文件','.txt')])as f:
show['text']=f.read()
Button(root,text='选择读取的文本文件',command=test1).pack()
show=Label(root,width=60,height=5,bg='green')
show.pack()
root.mainloop()
代码可以运行,但是选择完文本文件后就立马报错,报错信息如下:
Exception in Tkinter callback "e:/VS code files/GUI_Tkinter编程/GUI_37.py"
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py",
line 1892, in __call__ line 1892, in __call__
return self.func(*args)
File "e:/VS code files/GUI_Tkinter编程/GUI_37.py", line 6, in test1
with askopenfilename(title='上传文件',initialdir='d:',filetypes=[('文本文件','.txt')])as f:
AttributeError: __enter__
自己也弄了半天,没搞出来,请问老师怎么解决?