tkFileDialog 文件保存方法不起作用

发布于 2024-11-26 11:14:20 字数 1271 浏览 1 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-12-03 11:14:20

我很清楚,当您调用 时,您的文件句柄 self.lastopenself.lastsave 已设置为 False 的等效项>文件保存。您是否检查过它们在 fileopenfilesave 函数退出后仍然存在?通过这种方式进行调试非常简单,请尝试:

my_man = FileMan()
my_man.fileopen()
my_man.filesave()
print my_man.lastopen
print my_man.lastsave

如果这不起作用,请尝试使用此结果更新您的问题,我们将从那里开始。另外,您应该检查是否:

print my_man.lastopen == False and my_man.lastsave == False

Pretty clear to me that your file handles self.lastopen and self.lastsave are set to some equivalent of False by the time you call filesave. Did you check that they persist after your fileopen and filesave functions exit? Pretty simple to debug this way, try:

my_man = FileMan()
my_man.fileopen()
my_man.filesave()
print my_man.lastopen
print my_man.lastsave

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:

print my_man.lastopen == False and my_man.lastsave == False
当爱已成负担 2024-12-03 11:14:20

我明白了,我没有关闭文件。愚蠢的我。

I figured it out, I wasn't closing the file. Silly me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文