0

I am working with GTK in Python. I noticed that it is easy to SAVE AS but for some reason I cannot just SAVE a file. I thought I'd set it up to check if it was saved at all and then SAVE but for some reason it restarts the function over and over w/o saving the value of filename. The thing that is killing me is this feels like a beginner mistake. somehow I have forgotten to keep the value after the function has been run. I hope this makes sense.

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
gpoo
  • 8,408
  • 3
  • 38
  • 53
LUser
  • 1,127
  • 4
  • 23
  • 39
  • Your code indentation was completely messed up and thus hard to read. You had tabs and spaces mixed. Please go with [PEP 8](http://www.python.org/dev/peps/pep-0008/) and use 4-space indentation. Could you please make sure that I didn't change the flow of your code with my edit? – Thorsten Kranz Jan 17 '13 at 07:17

1 Answers1

1

As far as I can tell, you don't persist your filename in any way between the calls. You call filename = chooser.get_filename() on a newly created FileChooseDialog - this will always return None.

As I don't know more about the architecture of your program, I can just propose using a global variable to persist the filename, e.g.:

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

But remember, altering global variables is always dangerous, maybe you can think of a better approach. If you put your method into a class, you could make filename an attribute of this class, e.g., self._filename.

Another remark: Don't ever duplicate code. In the if-branch and in the elif-branch, you call

        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()

Put this into a separate method. I promise you'll forget to change the second occurrence of this code snippet if you ever come to change your file-writing.

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • Making them global did the trick. You're right making the classes should work and keep getting the value from the class. If anyone plans to make it global.I hit a point that to save w/o having to have opened a file and I just had to except NameError: and call the SaveAs Function. Hopefully it will help the next person down the way.Thanks Thorsten Kranz – LUser Jan 20 '13 at 06:08