在插件QGIS中编写和另存为python

时间:2013-12-24 18:07:30

标签: python qgis

我尝试在QGIS中制作插件,我想将另存为按钮,但是我收到了这样的错误消息

Traceback (most recent call last):
  File "C:\Users\Mr.Pakde/.qgis2/python/plugins\latih\latihdialog.py", line 69, in 

saveAs
    if not filename.isEmpty():
AttributeError: 'unicode' object has no attribute 'isEmpty'

我写这样的代码:

cariButton = self.ui.btnCari
QtCore.QObject.connect(cariButton,QtCore.SIGNAL('clicked()'),self.cari)
saveButton = self.ui.btnSave     
QtCore.QObject.connect(saveButton, QtCore.SIGNAL('clicked()'),self.saveAs)   

def  cari(self, event=None):
    #open dialog
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '*.xml')         

self.ui.lineFile.setText(filename)
    #panggil isi data

self.isiDataFile(filename)

def isiDataFile(self, nmfile):
        #buka dengan open mode baca
        teksFile = open(nmfile, 'r').read()

self.ui.textFile.setText(teksFile)

def _save(self, filename):        
    f = open( filename, "w" )
        f.write( "%s" % self.lineFile.text() )
f.close()
def savefile(self):
     if self.filename:
          self._save( "%s" % self.filename )
else:
          self.saveAs()

def saveAs(self):
        filename = QtGui.QFileDialog(self).getSaveFileName()
        if not filename.isEmpty():
            _filename = "%s" % filename
            self._save( _filename )
            self.setFilename( _filename )

我尝试以不同的格式保存文件

1 个答案:

答案 0 :(得分:4)

错误消息告诉您完全问题是什么:

   filename = QtGui.QFileDialog(self).getSaveFileName()
    if not filename.isEmpty():

filenameunicode对象,没有isEmpty()方法。尝试:

 if filename != "":

或,empty strings are False-y

if filename: