问题描述
我正在做一个文本编辑器,并且正在使用“保存/另存为”按钮,发现了一些我最初认为可行但后来发现有问题的东西。
菜单中的“另存为”和“另存为”按钮不会将文本小部件中的文本保存到文件中,而只是创建一个。
我已经将两个函数都设置为self.f1.write(text)
但只有单击几次后, 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()
1楼
您的代码正在保存数据。 但是,您不会关闭文件和python缓冲区输出,因此,磁盘上的文件实际上可能没有任何数据,除非有某种原因导致文件被关闭(例如正常退出程序)。
一种简单的解决方案是确保在写入文件后将其关闭,而最简单的方法是通过with 语句使用open
。
def save(self):
try:
with open(self.file, "w+") as f1:
texts = self.t1.get("1.0", "end-1c")
f1.write(text)
...
在上面的代码中,当with
块完成时,可以确保关闭文件并将上下文刷新到磁盘。
注意:您对索引0.0
使用不正确。
索引必须是一个字符串,并且第一个字符是"1.0"
而不是"0.0"
。
另外,Tkinter总是向文本小部件添加额外的换行符。
如果要确切地输入用户输入的内容,则需要获取除最后一个字符以外的所有字符,这就是"end-1c"
含义(结尾,减去一个字符)。