我正在使用 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