2013-01-17 75 views
0

我正在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 
+0

你的代碼縮進是完全搞砸了,因此很難閱讀。你有標籤和空間混合。請與[PEP 8](http://www.python.org/dev/peps/pep-0008/)一起使用並使用4空格縮進。你能確保我沒有改變我的編輯代碼的流程嗎? –

回答

1

據我所知,你不要在任何調用之間保持你的文件名。您在新創建的FileChooseDialog上調用filename = chooser.get_filename() - 這將始終返回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分枝,並在elif分枝,你叫

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

將這個變成一個獨立的方法。我保證,如果你想改變你的文件寫法,你會忘記更改這段代碼的第二次出現。

+0

讓他們成爲全球的伎倆。你是對的,這些班應該工作,並不斷從班上獲得價值。如果有人打算使它成爲全球性的。我點了一個保存w/o不得不打開一個文件的點,我只需要除了NameError:並調用SaveAs函數。希望它能幫助下一個人。感謝Thorsten Kranz – ApertureSecurity