[英]Save As error 'X' object has no attribute 'Y' in QGIS plugin
I try to make plugin to open, read and then save save in different format, i open .xml file and try to save as / write it in text or pdf format, but i get error message like this : 我尝试使插件打开,读取,然后以不同的格式保存,我打开.xml文件并尝试以文本或pdf格式另存为/写入,但是我收到如下错误消息:
File "C:\Users\Mr.Pakde/.qgis2/python/plugins\latih\latihdialog.py", line 71, in saveAs
self._save( _filename )
File "C:\Users\Mr.Pakde/.qgis2/python/plugins\latih\latihdialog.py", line 59, in _save
f.write( "%s" % self.nmfile.text() )
AttributeError: 'latihDialog' object has no attribute 'nmfile'
This is my code 这是我的代码
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, simpan):
f = open( simpan, "w" )
f.write( "%s" % self.nmfile.text() )
f.close()
def savefile(self):
if self.simpan:
self._save( "%s" % self.simpan )
else:
self.saveAs()
def saveAs(self):
tulis = QtGui.QFileDialog(self).getSaveFileName()
if tulis !="":
_filename = "%s" % tulis
self._save( _filename )
self.setFilename( _filename )
You are trying to use the variable nmfile
as a instance variable by addressing it in the way self.nmfile
. 您正在尝试使用变量nmfile
通过的方式解决它作为一个实例变量self.nmfile
。 However, this variable/attribute has never been initialized (compare: AttributeError: 'latihDialog' object has no attribute 'nmfile'). 但是,此变量/属性从未初始化过(比较:AttributeError:'latihDialog'对象没有属性'nmfile')。
You use the variable in a local context in isiDataFile, but as soon as this method ends, the local variable is lost and cannot be accessed any more. 您可以在isiDataFile的本地上下文中使用该变量,但是一旦此方法结束,该本地变量将丢失并且无法再访问。
You have to design the flow of your code, that it either will 您必须设计代码流,要么
_save
-method is called ( eg self.nmfile = '/tmp/myfile'
) 在调用_save
-method之前分配此实例变量(例如self.nmfile = '/tmp/myfile'
) nmfile
to the _save
-method, so it will be available there 将参数nmfile
传递给_save
-method,因此在那里可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.