【发布时间】:2018-01-31 11:03:17
【问题描述】:
我正在尝试在 Python 的 Tkinter 中为 scrolledText 创建一个导入函数,但是在读取文件时,会引发 AttributeError。代码:
def open_command():
openfile = tkFileDialog.askopenfilename()
if openfile != None:
contents = openfile.read()
textPad.delete('1.0', END)
textPad.insert('1.0', contents)
openfile.close()
错误:
contents = openfile.read()
AttributeError: 'unicode' object has no attribute 'read'
我想澄清一下'textPad' 指的是一个'ScrolledText' 对象。 有谁知道为什么会这样?起初我以为错误可能来自编码,所以我用 UTF-8 编码,但它仍然返回相同的错误。提前致谢!
【问题讨论】:
-
askopenfilename在文件名上返回一个文件 _name. You can't callread()`,这正是错误告诉你的内容。
标签: python unicode tkinter attributeerror