如何将tkFileDialog.asksaveasfilename()转换为缓冲区的字符串?

时间:2015-10-27 06:32:41

标签: python tkinter

我在Mac上创建了一个文本编辑器,但是在保存文件时遇到了问题。我可以打开,保存和保存文件,但是在我调用self.close后保存功能停止工作。

我收到此错误:

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

如何解决错误?

这是我的代码:

#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()

1 个答案:

答案 0 :(得分:1)

我猜你在点击self.saveas按钮之前就已经save了。问题在于self.saveas您将self.file保存为打开的文件,在行中 -

self.file=open(self.savelocation, "w+")

之后,当您点击save时,您尝试在此文件上再次使用open(),这不起作用,您需要传递文件的pathsavelocation?)而不是文件本身。示例 -

def save(self):
    try:
        text = self.t1.get("1.0", "end-1c")
        self.f1=open(self.savelocation, "w+")
        self.f1.write(text)
        self.f1.close()
    ... #Rest of the function.

此外,你try/except似乎搞砸了,试图做except AttributeError两次不会做你认为它做的事情。它不会捕获在第一个AttributeError块中导致的任何except

执行self.saveas不会调用该函数,如果要调用该函数,则应使用调用语法()作为self.saveas()。示例 -

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