tkFileDialog 文件保存方法不起作用
我刚刚开始使用 Tkinter 进行编程课程,但在使用文件对话框处理程序时遇到了一些问题。 fileopen 和filesaveas 方法可以正常工作,但filesave 方法则不能。 规范要求filesave方法应该保存到最后保存的文件;如果没有保存文件,则保存到最后打开的文件;否则保存为默认名称 quiz_spec.py。由于某种原因,前两个写入调用在到达时似乎没有保存文件(并且也没有生成任何错误。) 如果有人能告诉我为什么 filesaveas 和 filesave 中的相同保存调用的功能不同,并且还向我指出 tkFileDialog 保存函数的一个很好的示例,我将不胜感激。
class FileMan():
def __init__(self):
self.lastsave = None
self.lastopen = None
def fileopen(self):
handle = askopenfile(mode = 'r')
print "name of file you picked = "+str(handle.name)
self.lastopen = handle
print "first line of data from file: "+handle.readline()
def filesave(self):
if (self.lastsave):
self.lastsave.write("Save: Some data to save into the file\n")
elif (self.lastopen):
self.lastopen.write("Save: Some data to save into the file\n")
else:
handle = open('quiz_spec.py', 'w')
handle.write("Save: This is the new content of test.txt :-)")
def filesaveas(self):
handle = asksaveasfile(mode = 'w', defaultextension = '.py')
print "name of file you picked = "+str(handle.name)
self.lastsave = handle
handle.write("SaveAs: Some data to save into the file\n")
I have just started using Tkinter for a programming class and am having a bit of trouble using file dialog handlers. The fileopen and filesaveas methods work correctly, but the filesave method is not.
The specification requires that the filesave method should save to the last saved file; if no file has been saved, then save to the last opened file; else save to the default name quiz_spec.py. For some reason, the first two write calls don't seem to be saving save the file when they are reached (and aren't generating any errors either.)
It would be appreciated if someone could please tell me why the same save calls in filesaveas and filesave are functioning differently and also point me to a good example of a tkFileDialog save function.
class FileMan():
def __init__(self):
self.lastsave = None
self.lastopen = None
def fileopen(self):
handle = askopenfile(mode = 'r')
print "name of file you picked = "+str(handle.name)
self.lastopen = handle
print "first line of data from file: "+handle.readline()
def filesave(self):
if (self.lastsave):
self.lastsave.write("Save: Some data to save into the file\n")
elif (self.lastopen):
self.lastopen.write("Save: Some data to save into the file\n")
else:
handle = open('quiz_spec.py', 'w')
handle.write("Save: This is the new content of test.txt :-)")
def filesaveas(self):
handle = asksaveasfile(mode = 'w', defaultextension = '.py')
print "name of file you picked = "+str(handle.name)
self.lastsave = handle
handle.write("SaveAs: Some data to save into the file\n")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很清楚,当您调用
时,您的文件句柄
。您是否检查过它们在self.lastopen
和self.lastsave
已设置为False
的等效项>文件保存fileopen
和filesave
函数退出后仍然存在?通过这种方式进行调试非常简单,请尝试:如果这不起作用,请尝试使用此结果更新您的问题,我们将从那里开始。另外,您应该检查是否:
Pretty clear to me that your file handles
self.lastopen
andself.lastsave
are set to some equivalent ofFalse
by the time you callfilesave
. Did you check that they persist after yourfileopen
andfilesave
functions exit? Pretty simple to debug this way, try:If this doesn't work, try updating your question with the results of this and we'll take it from there. Also, you should check if:
我明白了,我没有关闭文件。愚蠢的我。
I figured it out, I wasn't closing the file. Silly me.