老师,请教一下,这是我的代码。test1返回的f值是路径加文件名,我本来想使用test2的方式将test1中的文件名使用f.name读取出来,就是test1中注释的部分,为什么不行呢?
"""文件对话框获取文件"""
from tkinter import *
from tkinter.filedialog import *
root = Tk()
root.geometry("500x300")
def test1():
f = askopenfilename(title="上传文件", initialdir="文档", filetypes=[("视频文件", ".mp4")])
print(f)
show["text"] = f
# with askopenfilename(title="上传文件", initialdir="文档", filetypes=[("视频文件", ".mp4")]) as f:
# print(f)
# show["text"] = f.name
# 参考的之前文件处理时的方式,如下:
# with open("e.txt", "r", encoding="utf-8") as f:
# print("文件名是:{0}".format(f.name)) # 返回文件名字
def test2():
with askopenfile(title="上传文件", initialdir="文档", filetypes=[("文本文件", ".txt")]) as f:
show["text"] = f.read()
Button(root, text="选择编辑的视频文件", command=test1).pack()
Button(root, text="选择需要读取的文本文件", command=test2).pack()
show = Label(root, width=40, height=3, bg="green")
show.pack()
root.mainloop()