0

我正在使用 Python 中的 GTK。我注意到另存为很容易,但由于某种原因,我不能只保存文件。我以为我会设置它以检查它是否已保存然后保存,但由于某种原因,它会一遍又一遍地重新启动函数,而不会保存文件名的值。杀死我的是这感觉像是一个初学者的错误。不知何故,我忘记在函数运行后保留该值。我希望这是有道理的。

def SaveFile(filename):
    chooser = gtk.FileChooserDialog("Save File...", None,
                                    gtk.FILE_CHOOSER_ACTION_SAVE,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 
                                     gtk.STOCK_SAVE, gtk.RESPONSE_OK))

    filename = chooser.get_filename()
    if filename == None:
        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            chooser.destroy()
            wbuffer = textview.get_buffer()
            text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
            openfile = open(filename,"w")
            openfile.write(text)
            openfile.close()
            print filename, "this is the first part"
            return filename
        else:
            chooser.destroy()
    elif filename != None:
        wbuffer = textview.get_buffer()
        text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
        openfile = open(filename,"w")
        openfile.write(text)
        openfile.close()
        print filename, "made it this far"
        return filename
    else:
        chooser.destroy()
        return filename
4

1 回答 1

1

据我所知,您不会在两次通话之间以任何方式保留您的文件名。您调用filename = chooser.get_filename()一个新创建的 FileChooseDialog - 这将始终返回None

由于我对您的程序的体系结构不了解更多,我可以建议使用全局变量来保存文件名,例如:

filename = None

def SaveFile(filename):
    global filename
    if filename == None:
        chooser = gtk.FileChooserDialog("Save File...", None,
                                    gtk.FILE_CHOOSER_ACTION_SAVE,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 
                                     gtk.STOCK_SAVE, gtk.RESPONSE_OK))
        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            chooser.destroy()
            wbuffer = textview.get_buffer()
            text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
            openfile = open(filename,"w")
            openfile.write(text)
            openfile.close()
            print filename, "this is the first part"
            return filename
        else:
            chooser.destroy()
    elif filename != None:
        wbuffer = textview.get_buffer()
        text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
        openfile = open(filename,"w")
        openfile.write(text)
        openfile.close()
        print filename, "made it this far"
        return filename
    else:
        chooser.destroy()
        return filename

但是请记住,更改全局变量总是很危险的,也许您可​​以想出更好的方法。如果你把你的方法放到一个类中,你可以filename为这个类创建一个属性,例如,self._filename

另一句话:永远不要重复代码。在if-branch 和elif-branch 中,您调用

        wbuffer = textview.get_buffer()
        text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
        openfile = open(filename,"w")
        openfile.write(text)
        openfile.close()

把它放到一个单独的方法中。我保证如果你改变你的文件编写,你会忘记改变这个代码片段的第二次出现。

于 2013-01-17T07:22:55.003 回答