【问题标题】:Python AttributeError : Object has no attribute in QGIS PluginPython AttributeError:对象在 QGIS 插件中没有属性
【发布时间】:2017-11-14 12:41:36
【问题描述】:

我正在尝试使用 QT Designer 创建一个 GUI。我已将我的 .ui 设计器文件转换为 .py

这是我的代码:

from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication 
from PyQt4.QtGui import QAction, QIcon
from qgis.core import *
import resources
from delete_feature_dialog import DeleteFeatureDialog
import os.path

class DeleteFeature:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # Declare instance attributes
        self.actions = []
        self.menu = self.tr(u'&DeleteFeature')
        self.toolbar = self.iface.addToolBar(u'DeleteFeature')
        self.toolbar.setObjectName(u'DeleteFeature')

    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):

        # Create the dialog (after translation) and keep reference
        self.dlg = DeleteFeatureDialog()

        ....

        return action

    def initGui(self):  
        icon_path = ':/plugins/DeleteFeature/icon.png'
        self.add_action(
            icon_path,
            text=self.tr(u''),
            callback=self.run,
            parent=self.iface.mainWindow())

    def run(self):
        #this code will populate the combo box with all vector layer
    self.dlg.layerListCombo.clear()
    layers = self.iface.legendInterface().layers()
    layer_list = []
    for layer in layers:
            layerType = layer.type()
            if layerType == QgsMapLayer.VectorLayer:
                layer_list.append(layer.name())
    self.dlg.layerListCombo.addItems(layer_list)
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.
            selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()
            selectedLayer = layers [selectedLayerIndex]
            .....

然后当我打开插件时,我收到以下错误:

'DeleteFeatureDialog' objectObject 没有属性 QGIS插件中的'layerlistcombo'

对此有任何建议。

【问题讨论】:

  • 您在此对话框中有一个组合框?您的对象名称是 layerlistcombo?可能没有。

标签: python qgis qpython


【解决方案1】:

好像你写的:

selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()

但你应该写:

selectedLayerIndex = self.dlg.layerListCombo.currentIndex()

就像您之前在代码中所做的一样(在编写时请注意骆驼符号,而不仅仅是小写字母),这可能会导致您收到 No Attribute 错误。

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 2021-08-16
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2020-09-11
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多