我正在尝试用Python和Tkinter获取一个文件,这是我所拥有的:
file1 = ""
file2 = ""
file1entry = Entry().pack()
file2entry = Entry().pack()
def OpenFile1():
name = askopenfilename(initialdir="C:/Users/",
filetypes=(("Text File", "*.txt"),
("All Files", "*.*")),
title="Choose a file."
)
# Using try in case user types in unknown file or closes without choosing a file.
try:
with open(name, 'r') as UseFile:
file1 = UseFile
file1entry.text = file1
except:
print("No file exists")
无论我尝试了什么,当选择一个文件时,我总是得到No file exists
我做错什么了?你知道吗
file1entry不在函数的作用域中,因此当您试图定义其
.text
属性时,会收到一个错误。你知道吗Python要求您显式地声明变量是否是全局的。你知道吗
如果你把线
在函数的顶部,它应该可以工作。你知道吗
这将使新代码:
相关问题 更多 >
编程相关推荐