我正在Python中使用GTK。我注意到保存起來很容易,但由於某種原因,我不能保存一個文件。我想我會設置它來檢查它是否已經保存,然後保存,但由於某種原因它會重新啓動函數而不是保存文件名的值。這件事情讓我非常難過,這就像是初學者的錯誤。不知何故,我忘記了在函數運行之後保留這個值。我希望這是有道理的。python gtk save gtksourceview
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
你的代碼縮進是完全搞砸了,因此很難閱讀。你有標籤和空間混合。請與[PEP 8](http://www.python.org/dev/peps/pep-0008/)一起使用並使用4空格縮進。你能確保我沒有改變我的編輯代碼的流程嗎? –