Why does this error appear?
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python开发者_JAVA百科27_1\lib\lib-tk\Tkinter.py", line 1410, in __call_
return self.func(*args)
File "editor.py", line 90, in onOpen
fileopened = open(fno, "w+")
TypeError: coercing to Unicode: need string or buffer, file found
Code on:
https://code.google.com/p/childreneditor/source/browse/trunk/editor-new.py
askopenfile
returns the opened file to you, not its name, so there's no need to call open
on it. If you really want the name, you can use askopenfilename
instead, but it doesn't seem necessary for what you're doing.
It's just like it says:
fileopened = open(fno, "w+")
That's the line of code that had a problem.
TypeError: coercing to Unicode: need string or buffer, file found
That's what the problem was.
w+
is a string, so clearly it's fno
that causes the problem. The problem is that a string or buffer
is needed, and it's actually a file
. You need a string or buffer
because that's the file-name parameter for open
. The purpose of open
is to open files given a file name; but you already have a file.
So just use the file.
Well when you open a file, you need the path... the fno is a class, but if you do: fileopened = open(fno.name, "w+") it should do the trick...
fno.name gives you the path with which you opened the file with the askopenfile.
"fno" in your code is a file object and open() expects receive string or unicode see http://docs.python.org/library/functions.html#open
精彩评论