【发布时间】:2015-10-26 06:31:47
【问题描述】:
我正在制作一个文本编辑器,我正在处理保存/另存为按钮,并且发现了一些我最初认为可行但后来发现问题的东西。菜单中的保存和另存为按钮不会将文本小部件中的文本保存到文件中,而只是创建一个。我已经输入了两个函数self.f1.write(text),但只有在点击几下后,文本才会真正保存。这不是时间问题,因为我等了大约五分钟,但它仍然没有工作。我在优胜美地有一台 Mac。
为什么不起作用?
这是脚本:
#modules
from Tkinter import *
from Tkinter import TclError
import tkFont
import tkMessageBox
import tkFileDialog
class Main(object):
def __init__(self, root):
root.title("PyText")
#menu for the file cascade
self.m1=Menu(root)
self.appmenu = Menu(self.m1, name="apple", tearoff=0)
self.m1.add_cascade(menu=self.appmenu)
self.appmenu.add_command(label="About PyText")
self.appmenu.add_separator()
self.fm=Menu(self.m1, tearoff=0)
self.fm.add_command(label="New", command=self.saveas)
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_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)
# Here is the problem.
# this command creates the file but does not
# save the text to the file.
def saveas(self):
text = self.t1.get(0.0, END)
self.savelocation=tkFileDialog.asksaveasfilename()
self.file=open(self.savelocation, "w+")
self.file.write(text)
# this also has
# the same problem. Once save as has
# been called, it does not save when pressed
# in first click but after a few clicks it
# finaly saves.
def save(self):
try:
text = self.t1.get(0.0, END)
self.f1=open(self.file, "w+")
self.f1.write(text)
except IOError:
text = self.t1.get(0.0, END)
self.f1=open(self.savelocation, "w+")
self.f1.write(text)
except Exception:
tkMessageBox.showinfo("Error", "Please save-as first.")
raise
#works fine!
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)
root = Tk()
app = Main(root)
root.mainloop()
【问题讨论】:
-
使用 close() 或上下文管理器。
-
我的意思是在保存后关闭文件,使用 close() 或上下文管理器,即
with file as f:。 -
感谢您为我节省了很多时间!你能把它放在答案中以便我接受吗?
-
如果“#these two don't work first time”表示你试图多次保存到同一个文件,问题是打开文件为“w+”,这意味着读写.如果您想将记录添加到现有文件,请使用 "a"=append to an existing file 打开它。