首页 > 解决方案 > TypeError:'dict_values'对象在从组合框调用层时不支持索引-pyqgis-qgisplugin

问题描述

当我从组合框中调用图层时,会抛出这样的错误。执行 Python 代码时发生错误:

TypeError: 'dict_values' 对象不支持索引 Traceback (最近一次调用最后): selectedLayer = layers[selectedLayerIndex1].layer() TypeError: 'dict_values' 对象不支持索引

Python 版本:3.7.0(v3.7.0:1bf9cc5093,2018 年 6 月 27 日,04:59:51)[MSC v.1914 64 位(AMD64)] QGIS 版本:3.4.15-Madeira Madeira,e83d02e274

代码 selectedLayerIndex1 = self.dlg.comboBox.currentIndex()

selectedLayer = 层[selectedLayerIndex1].layer()

qgis vrsion:QGIS 版本:3.4.15-Madeira Madeir

标签: python

解决方案


如果要使用 Index,则必须先迭代图层并将其添加到图层列表中。只有在 thar 之后,您才能使用索引并从列表中选择图层(使用索引)

在 def 中使用此代码run(self)

layers_list = []
curLayers = QgsProject.instance().mapLayers().values()
for layer in curLayers:
    layers_list.append(layer)


def chooseIndex():
    sLayerIndex = self.dockwidget.comboBox.currentIndex()
    selectedLayer = layers_list[sLayerIndex]
    print(selectedLayer)

self.dockwidget.comboBox.currentIndexChanged.connect(chooseIndex)


推荐阅读