I am creating a text editor on a Mac and am having trouble with file saving. I can open, save as and save files but the save function stops working after I call self.close. I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "Test1.py", line 43, in save
    self.f1=open(self.file, "w+")
TypeError: coercing to Unicode: need string or buffer, file found

How can I get the directory as a string of buffer to fix the error?

Here is my code:

#modules
from Tkinter import *
from Tkinter import TclError
import tkMessageBox
import tkFileDialog
import os
#main class
class Main(object):
    def __init__(self, root):
        root.title("PyText")
        #menu for the file cascade
        self.m1=Menu(root)

        self.fm=Menu(self.m1, tearoff=0) 
        self.fm.add_command(label="Open", accelerator="Cmd+O", command=self.open)
        #these two don't work first time...
        self.fm.add_command(label="Save", accelerator="Cmd+S", command=self.save)
        self.fm.add_command(label="Save As", command=self.saveas)

        self.fm.add_command(label="Close", command=self.close)
        self.fm.add_separator()
        self.fm.add_command(label="Exit", command=root.quit)
        self.m1.add_cascade(label="File", menu=self.fm)

        root.config(menu=self.m1)

        #Main text widget
        self.t1=Text(root)
        self.t1.config(width=90, height=40, undo=True, highlightbackground="black", cursor="ibeam")
        self.t1.grid(row=1)
        self.t1.focus_set()

    def saveas(self):
        text = self.t1.get("1.0", "end-1c")
        self.savelocation=tkFileDialog.asksaveasfilename()
        self.file=open(self.savelocation, "w+")
        self.file.write(text)
        self.file.close()

    def save(self):
        try:
            text = self.t1.get("1.0", "end-1c")
            self.f1=open(self.file, "w+")
            self.f1.write(text)
            self.f1.close()
        except AttributeError:
            text = self.t1.get("1.0", "end-1c")
            self.f1=open(self.savelocation, "w+")
            self.f1.write(text)
            self.f1.close()
        except AttributeError:
            text = self.t1.get("1.0", "end-1c")
            self.f1=open(self.savelocation1, "w+")
            self.f1.write(text)
            self.f1.close()
        except Exception:
            self.saveas
            raise

    def open(self):
        self.file=tkFileDialog.askopenfilename()
        self.OpenFile=file(self.file) # get a file handle
        self.ReadFile= self.OpenFile.read() # read the file to variable
        self.OpenFile.close() # close file handle
        self.t1.delete(0.0, END)
        self.t1.insert(END, self.ReadFile)

    def close(self):
        self.t1.delete(0.0, END)
        tkMessageBox.showinfo("New file", "Closing old file and creating a new one.")
        self.savelocation1=tkFileDialog.asksaveasfilename()
        self.file=open(self.savelocation1, "w+")
        self.file.close()

root = Tk()
app = Main(root)
root.mainloop()

You have

self.file
self.OpenFile
self.ReadFile
self.savelocation
self.savelocation1

That makes a lot of file-related members. Try to use them coherently, for example at line 61, self.file is a string, but at line 36 it is a file object.
Use different variables to store filenames and file objects. Also note that file objects often have a member .name which allows to access their filename.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.