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
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.